对于每个地区,显示人口至少为1000万的国家的地区和数量。
该表名为bbc,包含以下字段:
我以为我可以这样做:
SELECT region, COUNT(name) FROM bbc
GROUP BY region
HAVING population > 10000000
答案 0 :(得分:2)
如果您想显示所有区域,并且在每个区域内计算人口超过1000万的数字,那么这可能是最简单的:
SELECT region, SUM(CASE WHEN population > 10000000 THEN 1 ELSE 0 END) as BigCountries
FROM bbc
GROUP BY region
因此,如果您所在的地区没有任何国家/地区的人口数超过10000000,那么您仍然会有一个具有该地区名称的行和一个0。
从您的评论到@Yograj Gupta问题 - 如果您想要所有国家/地区拥有人口的地区> 10000000,那么你可以修改上面的内容:
SELECT region, COUNT(*) as Cnt,SUM(CASE WHEN population > 10000000 THEN 1 ELSE 0 END) as BigCountries
FROM bbc
GROUP BY region
HAVING COUNT(*) = SUM(CASE WHEN population > 10000000 THEN 1 ELSE 0 END)
或者只是利用更简单的属性:
SELECT region, COUNT(*) as Cnt,MIN(population) as LowestPop
FROM bbc
GROUP BY region
HAVING MIN(population) > 10000000
该地区任何国家的最低人口数> 10000000,那么所有国家的人口必须>千万
答案 1 :(得分:1)
你应该使用
-- For getting count of country name which has population > 10000000
SELECT region, COUNT(name) FROM bbc
WHERE population > 10000000
GROUP BY region
或
-- For getting region which has SUM(population) > 10000000
SELECT region, COUNT(name) FROM bbc
GROUP BY region
HAVING SUM(population) > 10000000