达梦数据库把日志数据按天统计不同状态的数据,实现字段行转列与根据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字段对数据进行筛选就可以了

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值