mysql中聚合函数条件
来源:互联网 发布:预防数据库死锁的方法 编辑:程序博客网 时间:2024/04/27 15:32
有个需求,某张表,有个状态字段(1:成功,2:失败,类似这样的),现要用日期分组统计不同状态下的数量
先写了个子查询:
select aa.logDate,aa.totalLogs
,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=1) pendingLogs
,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=2) successLogs
,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=3) errorLogs
,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=4) callbackErrorLogs
from
(
select
DATE_FORMAT( a.startTime, '%Y-%m-%d') logDate,
count(1) totalLogs
from dxp.dxp_handlermodel a
group by DATE_FORMAT( a.startTime, '%Y-%m-%d')
) aa
执行相当慢,想到count中能不能加条件,找了一下,如下:
select
DATE_FORMAT( startTime, '%Y-%m-%d') logDate,
count(1) totalLogs,
count(if(executeStatus=1,true,null)) pendingLogs,
count(if(executeStatus=2,true,null)) successLogs,
count(if(executeStatus=3,true,null)) errorLogs,
count(if(executeStatus=4,true,null)) callbackErrorLogs
from dxp.dxp_handlermodel
group by DATE_FORMAT( startTime, '%Y-%m-%d')
简明易懂,且执行效率非常高
其它的聚合函数也可以用,如SUM等
居然之前不知道,真不知道当年的报表项目是咋做的,记录一下
- mysql中聚合函数条件
- MySQL中聚合函数的使用
- Mysql 与聚合函数在一起时候where条件和having条件的过滤时机
- Mysql聚合函数简介
- MySQL中的聚合函数
- mysql聚合函数
- mysql(三、聚合函数)
- Mysql 使用聚合函数
- mysql聚合函数
- MySQL聚合函数
- MySQL聚合函数
- Mysql-聚合函数
- Mysql之聚合函数
- mysql排序查询,多条件排序查询,聚合函数的使用,分组统计查询,
- oracle 中如何在使用sum或其他聚合函数中,select查询条件处如何使用判断条件
- mysql与oracle中分组、聚合函数的区别!
- MySQL中聚合函数对NULL值处理
- MySQL中聚合函数count的使用和性能优化
- iOS统计代码总行数
- LOD你好,LOD再见
- BLE-NRF51822教程2-工程初始化流程
- iOS笔记 复习Category和Extension
- Deep Belief Networks资料汇总
- mysql中聚合函数条件
- 关于XAMPP下使用PHPBB或者Discuz!
- JQUERY基础知识总结
- ViewPager或ImgeView加载图片出现内存溢出(OOM)
- 安卓开发1--搭建最新开发环境及新建项目检测
- java读取properties文件的几种方法
- sqlserver数据库收缩日志并限制最大增长
- Google官方Android API指南索引
- 【Lua】观察者模式在Lua中的实现