hi宝贝网站建设那家好,长沙市网站制作多少钱,怎么样备份网站数据库,视频网站开发策划书1、这是日志表记录的数据#xff0c;现在需要统计出每个app_id各个警告类型alarm_type的总数 2、先实现行转列#xff0c;把三个alarm_type值转成列字段
SQL
select app_id,count(CASE WHEN alarm_typeconcurrency THEN 1 ELSE null END) AS currentCount,count(CASE WHEN …1、这是日志表记录的数据现在需要统计出每个app_id各个警告类型alarm_type的总数 2、先实现行转列把三个alarm_type值转成列字段
SQL
select app_id,count(CASE WHEN alarm_typeconcurrency THEN 1 ELSE null END) AS currentCount,count(CASE WHEN alarm_typeexception THEN 1 ELSE null END) AS exceptionCount,count(CASE WHEN alarm_typetimeout 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_typeconcurrency THEN 1 ELSE null END) AS currentCount,count(CASE WHEN aal.alarm_typeexception THEN 1 ELSE null END) AS exceptionCount,count(CASE WHEN aal.alarm_typetimeout THEN 1 ELSE null END) AS timeoutCountfrom ZYB_SJML.app_alarm_log aal group by aal.app_id, aal.alarm_type) aaa
group by aaa.app_id
最终效果
需要根据日期查询的话在子查询里面根据create_time字段对数据进行筛选就可以了