1、这是日志表记录的数据,现在需要统计出每个app_id各个警告类型alarm_type的总数
2、先实现行转列,把三个alarm_type值转成列字段
SQL
select
app_id,
count(CASE WHEN alarm_type='concurrency' THEN 1 ELSE null END) AS currentCount,
count(CASE WHEN alarm_type='exception' THEN 1 ELSE null END) AS exceptionCount,
count(CASE WHEN alarm_type='timeout' THEN 1 ELSE null END) AS timeoutCount
from ZYB_SJML.app_alarm_log aal
group by app_id, alarm_type
从实现效果可以看到,把concurrency、exception、timeout的统计总数转为了列字段。但是相同app_id不同的alarm_type统计数没有合成一条数据
3、把步骤2的SQL作为子查询,把相同app_id的统计数据合成一条
SQL
select
aaa.app_id,
sum(currentCount) currentCount,
sum(exceptionCount) exceptionCount,
sum(timeoutCount) timeoutCount from
(select
aal.app_id,
count(CASE WHEN aal.alarm_type='concurrency' THEN 1 ELSE null END) AS currentCount,
count(CASE WHEN aal.alarm_type='exception' THEN 1 ELSE null END) AS exceptionCount,
count(CASE WHEN aal.alarm_type='timeout' THEN 1 ELSE null END) AS timeoutCount
from ZYB_SJML.app_alarm_log aal
group by aal.app_id, aal.alarm_type) aaa
group by aaa.app_id
最终效果
需要根据日期查询的话,在子查询里面根据create_time字段对数据进行筛选就可以了