我是sql的新手。我有一个表格数据
district city state population
d1 c1 s1 2000
d2 c1 s1 10000
d3 c1 s1 400000
d1 c2 s2 500000
我想拥有人口超过41万的城市数量
所以我想要的输出应该是2,因为有两个城市c1和c2。我想首先按城市进行分组,然后计算每个城市的人口总和,然后检查其人口是否大于410000,所以我使用了查询
select count(city) from city_table group by city having sum(population) > 410000;
但我得到的输出是
count(city)
3
1
请告诉我的查询有什么问题
您应该使用两个级别的聚合:
select count(*)
from (select city, sum(population) as population
from city_tabl
group by city
having sum(population) > 410000
) c;