我有一张包含一些遗留数据的表格,我怀疑这些数据可能有些混乱。这是一个多对多的连接表。
LIST_MEMBERSHIPS
----------------
list_id
address_id
我想运行一个查询,它会计算每个list_id
- address_id
对的出现次数,并显示从最高到最低出现次数的每次出现次数。
我知道它必须涉及COUNT()
和GROUP BY
,对吧?
答案 0 :(得分:4)
select list_id, address_id, count(*) as count
from LIST_MEMBERSHIPS
group by 1, 2
order by 3 desc
您可能会发现添加
很有用having count > 1
答案 1 :(得分:1)
select count(*), list_id, address_id
from list_membership
group by list_id, address_id
order by count(*) desc