《SQL 进阶教程》case :用一条 SQL 语句进行不同条件的统计
进行不同条件的统计是case表达式的著名用法之一
select
name,
sum(case when sex = 1 then population else 0 end) as cnt_m,
sum(case when sex = 2 then population else 0 end) as cnt_f
from city_population_copy
group by name
实现了行转列
进行不同条件的统计是case表达式的著名用法之一
select
name,
sum(case when sex = 1 then population else 0 end) as cnt_m,
sum(case when sex = 2 then population else 0 end) as cnt_f
from city_population_copy
group by name
实现了行转列