Case When和聚合函数count、sum的使用

xiaoxiao2021-02-28  23

需求:

根据贫困标志,取出不同标准下的的人数有多少,以区县位单位。

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 )

结果展示:

瘦子没有夏天 认证博客专家 博客专家 Java深度用户 大家好,我是Leon。一个专业与Java领域的程序员,偶尔理理财。欢迎大家来阅读我博客,如果你发现极个别文章和书籍内容雷同,请叫我勤奋的code搬运工٩(๑❛ᴗ❛๑)۶。我的邮箱:gyoomi0709@foxmail.com
转载请注明原文地址: https://www.6miu.com/read-2632713.html

最新回复(0)