嗨,我是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
我怎样才能为整张桌子做这件事?
答案 0 :(得分: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