计算具有不同列名的表中的重复记录

mey*_*edi 1 sql sql-server

嗨我是sql的新手,这是我的问题:我有这个表:

rhp1 ---- rhp2 ---- rhp3 ---- rhp4 --- rhp5 .....

51 ------- 32 ------ 61 ------ 54 ----- 32 ....

21 ------- 95 ------ 125 ----- 25 ----- 45 ......

65 ------- 58 ------- 58 ----- 69 ----- 25 ......

我想计算这个表中重复的每个字段值的数量!例如:第一场51,我们在这张桌子上有多少51,....

有了这个查询,我可以在一列中得到这个:

select rhp , count(1) as count_rhp from tbl_all 
group by rhp
order by count_rhp Desc 
Run Code Online (Sandbox Code Playgroud)

我怎么能在整个桌子上这样做?

Jau*_*ang 5

将所有列合并为一个,然后分组并计数:

with tbl_all (rhp) as
(
 select rhp1 from tbl union all
 select rhp2 from tbl union all
 select rhp3 from tbl union all
 select rhp4 from tbl union all
 select rhp5 from tbl 
)
select rhp , count(1) as count_rhp 
from tbl_all 
group by rhp
order by count_rhp Desc 
Run Code Online (Sandbox Code Playgroud)