需求:
根据贫困标志,取出不同标准下的的人数有多少,以区县位单位。
1代表已脱贫人口;2代表返贫人口;3代表未脱贫
select
t3.region_id as 'regionId',
t3.region_name as 'regionName',
count(case when t1.tricolor = 1 then 1 else null end ) as 'greenTotal',
count(case when t1.tricolor = 2 then 1 else null end ) as 'yellowTotal',
count(case when t1.tricolor = 3 then 1 else null end )as 'redTotal'
from pa_household_member t1
left join pa_household t2 on t2.household_id = t1.household_id
left join system_region t3 on t3.region_id = t2.area
group by t2.area
已知:
count(1) 算上一条记录 count(null)为0;利用此特性进行分类统计。不用写复杂的子查询。
要点:
count在套住case when ..... 例如: count (case when ....end )
结果展示: