Use of Case When and aggregate functions count and sum

need:

According to the poverty signs, the number of people under different standards is taken out, in the unit of district and county.

1 represents the population that has been lifted out of poverty; 2 represents the population that has returned to poverty; 3 represents the population that has not been lifted out of poverty

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

A known:

count(1) counts a record count (null) as 0; use this feature for classification statistics. No need to write complex subqueries.

Key Points:

count is in case when ..... eg: count (case when ....end )

Results show:


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326825658&siteId=291194637