--1 select country, sum(case when sex = 1 then 1 else 0 end) as 男, sum(case when sex = 0 then 1 else 0 end) as 女 from demo group by country;--2 select country,count(1) from demo group by country;
一:SELECT country, SUM(CASE WHEN sex = 1 THEN POP ELSE 0 END) AS 男人口数量, SUM(CASE WHEN sex = 2 THEN POP ELSE 0 END) AS 女人口数量 FROM demo GROUP BY country二:洲是什么?亚洲?
对就这几条数据 用CASE WHEN 判断的
SQL> select zhou,sum(shul) from ( 2 select case when country = '中国' then '亚洲' 3 when country = '美国' then '北美洲' 4 when country = '加拿大' then '北美洲' 5 when country = '英国' then '欧洲' end as zhou ,sum(pop) shul 6 from demo 7 group by country) 8 group by zhou 9 /ZHOU SUM(SHUL) ------ ---------- 欧洲 94 北美洲 205 亚洲 580SQL>
select zhou,sum(shul) from ( select case when country = '中国' then '亚洲' when country = '美国' then '北美洲' when country = '加拿大' then '北美洲' when country = '英国' then '欧洲' end as zhou ,pop shul from demo ) group by zhou
--1
select country,
sum(case when sex = 1 then 1 else 0 end) as 男,
sum(case when sex = 0 then 1 else 0 end) as 女
from demo
group by country;--2
select country,count(1)
from demo
group by country;
SUM(CASE WHEN sex = 1 THEN POP ELSE 0 END) AS 男人口数量,
SUM(CASE WHEN sex = 2 THEN POP ELSE 0 END) AS 女人口数量
FROM demo
GROUP BY country二:洲是什么?亚洲?
2 select case when country = '中国' then '亚洲'
3 when country = '美国' then '北美洲'
4 when country = '加拿大' then '北美洲'
5 when country = '英国' then '欧洲' end as zhou ,sum(pop) shul
6 from demo
7 group by country)
8 group by zhou
9 /ZHOU SUM(SHUL)
------ ----------
欧洲 94
北美洲 205
亚洲 580SQL>
select case when country = '中国' then '亚洲'
when country = '美国' then '北美洲'
when country = '加拿大' then '北美洲'
when country = '英国' then '欧洲' end as zhou ,pop shul
from demo
)
group by zhou