2014-09-23 81 views
1

嗨,我在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 

我如何能做到这一点的整桌子?

+2

你的问题不是很清楚。你能告诉我们你想要的输出吗? – Andrew 2014-09-23 15:13:24

+0

rhp1/rhp2/rhp3/etc有什么区别吗?我认为这个问题很难回答的原因是因为他们真的不应该是第一个不同的专栏。 – Erik 2014-09-23 15:20:11

+0

不,他们是一样的!我为他们放1,2,3,4的共振是我不能在sql中为不同的列放置相同的rhp名称。它是一个有7个rhp的数据集,我想要统计整个数据集中有多少个字段值被重复。 – 2014-09-23 15:27:31

回答

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 
+0

这是最好的答案... – 2014-09-23 15:37:42

+0

thankssssss它的工作很好^ _ ^我得到了我的答案谢谢 – 2014-09-23 15:45:14

+0

@meysammotamedi,一定要检查标记下的投票标记为答案。 – paqogomez 2014-09-23 15:55:54