我有一些这样的数据
(AID,Answer)
(1,A)
(1,A)
(1,A)
(1,B)
(1,B)
(1,C)
(2,C)
(2,C)
(2,C)
(2,B)
(2,B)
(3,A)
我想按AID进行分组,并在每组中尽可能多地使用Answer。
结果:
(1,A)
(2,C)
使用SQL工具。
答案 0 :(得分:1)
模式是最常见的值。最简单的方法是使用聚合和row_number()
:
select aid, answer
from (select aid, answer, count(*) as cnt,
row_number() over (partition by aid order by count(*) desc) as seqnum
from t
group by aid, answer
) aa
where seqnum = 1;