SQL查找不同的值计数,重复值计数2次,重复值计数3次等

时间:2017-10-25 12:11:26

标签: sql amazon-redshift

例如,

AAA
BBB
BBB
CCC
BBB
CCC
DDD
DDD

有4个唯一值(AAA, BBB, CCC, DDD),2个重复值两次 (CCC, DDD),1次重复三次(BBB)。我想为这个问题写一个SQL查询。请帮忙

所以答案是4,2,1。

1 个答案:

答案 0 :(得分:1)

您可以使用row_number()

执行此操作
select, seqnum, count(*)
from (select col, row_number() over (partition by col) as seqnum
      from t
      group by col
     ) h
group by seqnum
order by seqnum;