mysql 统计 一行数据多个列 中各种值的 数量

表数据

 

统计每个name 的指标中红 / 黄 / 绿 / 灰 灯数量.   分组 使用sum()函数

 

select 
name,
zhibiao1 as '指标1',
zhibiao2 as '指标2',
zhibiao3 as '指标3',
sum(case when zhibiao1 ='' then 1 else 0 end) + sum(case when zhibiao2 ='' then 1 else 0 end)+sum(case when zhibiao3 ='' then 1 else 0 end)  as red_count ,
sum(case when zhibiao1 ='' then 1 else 0 end) + sum(case when zhibiao2 ='' then 1 else 0 end)+sum(case when zhibiao3 ='' then 1 else 0 end)  as yellow_count ,
sum(case when zhibiao1 ='绿' then 1 else 0 end) + sum(case when zhibiao2 ='绿' then 1 else 0 end)+sum(case when zhibiao3 ='绿' then 1 else 0 end)  as green_count ,
sum(case when zhibiao1 ='' then 1 else 0 end) + sum(case when zhibiao2 ='' then 1 else 0 end)+sum(case when zhibiao3 ='' then 1 else 0 end)  as gray_count
from info
group by name

 

 

 

posted @ 2023-11-28 21:34  Avicii_2018  阅读(91)  评论(0编辑  收藏  举报