当前位置:   article > 正文

达梦数据库把日志数据按天统计不同状态的数据,实现字段行转列与根据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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

从实现效果可以看到,把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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

最终效果
在这里插入图片描述

需要根据日期查询的话,在子查询里面根据create_time字段对数据进行筛选就可以了

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/881056
推荐阅读
相关标签
  

闽ICP备14008679号