达梦数据库把日志数据按天统计不同状态的数据,实现字段行转列与根据id分组

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字段对数据进行筛选就可以了

posted @ 2024-02-28 20:23  →_→BéLieve  阅读(81)  评论(0)    收藏  举报  来源