我有2列
A B
1 2
2 2
1 2
3 2
5 2
0 2
4 2
11 4
12 4
11 4
我希望SQL查询返回对(A,B),其中: B出现了3次或更多次 AND(A,B)是唯一的
结果表将是:
A B
1 2
2 2
3 2
5 2
0 2
4 2
答案 0 :(得分:0)
您可以使用具有按B分组的选定表的连接,其中count = 3
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*)= 3
) t2 on t2.b = t1.b
和3或更多
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*) >= 3
) t2 on t2.b = t1.b