我的表格式为:
City Amount Name
New York 10,000 Joe
Boston 10,000 Tom
New York 15,000 Jev我正在寻找一个查询,它将返回至少有25个共同城市的人对的姓名和共同城市的确切数量。我的输出需要采用以下形式:
城市,Name1,Name2,计数(*)
我尝试了以下几种不同的变体:
select t1.city, t1.name, t2.name, count(*) from T t1 join T t2
on t1.city = t2.city
where count(*) > 25但我没那么走运。请给我建议。
发布于 2020-04-15 03:48:01
你已经很接近了。您需要一个GROUP BY和HAVING
select t1.city, t1.name, t2.name, count(*)
from T t1 join
T t2
on t1.city = t2.city
group by t1.city, t1.name, t2.name
having count(*) > 25;发布于 2020-04-15 03:56:14
我相信你可以使用窗口函数来获得你想要的东西:
SELECT city, name1, name2, count_of_cities
(
select t1.city, t1.name as Name1, t2.name as Name2, count(*) OVER (PARTITION BY t1.name, t2.name) as count_of_cities
from T t1 join T t2
on t1.city = t2.city
) dt
WHERE dt.count_of_cities > 25;https://stackoverflow.com/questions/61215738
复制相似问题