給定一張表
| 國家 | 人口 |
|---|---|
| 中國 | 600 |
| 印度 | 600 |
| 美國 | 200 |
| 韓國 | 600 |
| 澳大利亞 | 700 |
| 英國 | 400 |
求這樣的結(jié)果集:
| 州 | 人口 |
|---|---|
| 亞洲 | 3600 |
| 北美洲 | 400 |
| 大洋洲 | 1400 |
| 歐洲 | 800 |
--
建表sql:
CREATE TABLE country (
name VARCHAR(255) PRIMARY KEY,
population INT
);
INSERT INTO country (NAME, population)
VALUES
('中國', 600),
('印度', 600),
('美國', 200),
('韓國', 600),
('澳大利亞', 700),
('英國', 400);
sql:
SELECT sum(c.population) as '人口',
CASE c.`name`
when '中國' then '亞洲'
when '印度' then '亞洲'
when '美國' then '北美洲'
when '韓國' then '亞洲'
when '澳大利亞' then '大洋洲'
when '英國' then '歐洲'
else '其他'
end as '州'
from country c
GROUP BY
CASE c.`name`
when '中國' then '亞洲'
when '印度' then '亞洲'
when '美國' then '北美洲'
when '韓國' then '亞洲'
when '澳大利亞' then '大洋洲'
when '英國' then '歐洲'
else '其他' end;
結(jié)果:
