数据如下
需求:
对null和非null值进行分组统计
实现思路:
先使用子查询对null值使用coalesce函数进行赋默认值,作为标识。再去分组统计
select type,count(type) from (
select coalesce(type,0) as type from testtable
) as temp
GROUP BY type
效果如下
需求升级:
把结果跟字典表进行结合,并且字典表中没有默认值对应的名称
实现思路:
在原有的基础上结合case when … then … else … end关键字
select type,(case when dict.value is null then '零级' else dict.label end) as name, td.count from
(select type,count(type) as count from (
select coalesce(type,0) as type from testtable) as temp group by type
) as td
left join sys_dict as dict on td.type = dict.value
效果如下:
版权声明:本文为weixin_45535408原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。