我有一张桌子:
ID CLUSTERID
1 56
1 24
1 24
1 35
2 13
2 24
现在,我希望得到以下内容: 我想计算每个id,大多数时候哪个集群id重复。 例如,在ID = 1时,CLUSTERID = 24在大多数时间重复 在ID = 2中,我有2个重复相同的CLUSTER ID。 所以在输出中我会:
ID CLUSTERID
1 24
2 13
2 24
我写的答案(并且有效)
TT是我的原始表,有2列:ID和CLUSTER ID
SELECT t3.ID,t3.ClusterID,t3.ListingAmount
从
(SELECT ID,ClusterID,COUNT()AS ListingAmount
来自tt
GROUP BY ID,ClusterID)AS t3 LEFT JOIN
(SELECT ID,MAX(ListingAmount)AS数量
从
(SELECT ID,ClusterID,COUNT()AS ListingAmount
来自tt
GROUP BY ID,ClusterID)AS t2
GROUP BY ID)BB BB.id = t3.id
WHERE BB.amount = t3.ListingAmount
答案 0 :(得分:0)
现在想不出一个更优雅的解决方案(我确定有),但似乎可以做到这一点:
select t1.id,
t1.clusterid,
t1.cnt
from (
select id,
clusterid,
count(*) as cnt
from foo
group by id, clusterid
) t1
join (select id,
max(cnt) as max_count
from (
select id,
clusterid,
count(*) as cnt
from foo
group by id, clusterid
) tm
group by id
) t2 on t1.id = t2.id
and t1.cnt = t2.max_count
order by t1.id, t1.cnt;
SQLFiddle示例:http://sqlfiddle.com/#!2/2cacc/3