《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

实现了行转列

posted @ 2019-03-20 16:10  newlangwen  阅读(272)  评论(0编辑  收藏  举报