SELECT
Continent,
Region,
COUNT(*) AS CountriesCount,
AVG(Population) AS AvgPopulation
FROM country
GROUP BY Continent, Region
| Continent |
Region |
CountriesCount |
AvgPopulation |
| Asia |
Eastern Asia |
8 |
188,416,000.00 |
| Asia |
Middle East |
18 |
10,465,594.44 |
| Asia |
Southeast Asia |
11 |
47,140,090.90 |
| Asia |
Southern and Central Asia |
14 |
106,484,000.00 |
| Europe |
Baltic Countries |
3 |
2,520,633.33 |
| Europe |
British Islands |
2 |
31,699,250.00 |
| Europe |
Eastern Europe |
10 |
30,702,600.00 |
| Europe |
Nordic Countries |
7 |
3,452,342.85 |
| Europe |
Southern Europe |
15 |
9,644,946.66 |
| ... |
| GROUP BY 中可以写任意多个列,这样分组可以嵌套,对数据的分组方式控制得更细。
除聚合计算之外,SELECT 中出现的每一列都必须写进 GROUP BY。 |