mysql统计分组求和
mysql统计分组求和
1、由于工作需要对数据进行一个分组展示汇总处理,之前的处理情况如下:
SELECT
a.qxmc as ‘区县’,
count() as ‘下发数’,
SUM(b.sflxs is not null) as ‘核查数’,
SUM(b.sflxs is not null)/count() as ‘完成率’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as ‘查实数’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1)/SUM(b.sflxs is not null) as ‘查实率’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null) as ‘管控数’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null)/SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as ‘管控率’
FROM
t_jcxx a,t_hcryxx b where a.id=b.jcxxid
GROUP BY
a.qxmc
然后通过复制到Excel进行求和汇总处理,由于本人比较懒,所以想找一个直接出汇总的方法,想要的效果图如下:
百度找了很多很多用例,基本没有找到相符合业务场景的sql,后面请教了一个大佬,通过拼接的方式弄出来了:SELECT
a.qxmc as ‘区县’,
count() as ‘下发数’,
SUM(b.sflxs is not null) as ‘核查数’,
SUM(b.sflxs is not null)/count() as ‘完成率’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as ‘查实数’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1)/SUM(b.sflxs is not null) as ‘查实率’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null) as ‘管控数’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null)/SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as ‘管控率’
FROM
t_jcxx a,t_hcryxx b where a.id=b.jcxxid
GROUP BY
a.qxmc
union ALL
select ‘汇总’,sum(a.xfs),sum(a.hcs),sum(a.wcs)/10,sum(a.css),sum(a.csl),sum(a.gks),sum(a.gkl)/10 from (
SELECT
a.qxmc as ‘区县’,
count() as xfs,
SUM(b.sflxs is not null) as hcs,
SUM(b.sflxs is not null)/count() as wcs,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as css,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1)/SUM(b.sflxs is not null) as csl,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null) as gks,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null)/SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as gkl
FROM
t_jcxx a,t_hcryxx b where a.id=b.jcxxid
GROUP BY
a.qxmc
) a
执行结果如下:
但是这个sql太冗余了,很不喜欢,想对它进行优化,最后找了很久很久,终于让我找到了这个宝藏sql函数:COALESCE。说实话这个函数见都没见过,之前问了挺多人,他们的经验都是分组之后不能直接汇总求和了,最后竟然被这一个小小的函数解决了,果然只要想得到,就一定能找到方法,我们言归正传,优化后的sql为:
SELECT
COALESCE(a.qxmc,‘汇总’) as ‘区县’,
count() as ‘下发数’,
SUM(b.sflxs is not null) as ‘核查数’,
SUM(b.sflxs is not null)/count() as ‘完成率’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as ‘查实数’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1)/SUM(b.sflxs is not null) as ‘查实率’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null) as ‘管控数’,
SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1 and b.gkcs is not null)/SUM(b.sflxs=1 and b.sfchbl=1 and b.szdq=1) as ‘管控率’
FROM
t_jcxx a,t_hcryxx b where a.id=b.jcxxid
GROUP BY
a.qxmc WITH ROLLUP
我们来看一下运行结果:
完美契合,是我想要的结果。由于这么罕见的sql函数大家可能也跟我一样没有见过,所以特地发出来给大家安利一下!!!
觉得有用可以意思一下。
mysql统计分组求和相关推荐
- mysql 分组求和_MySql基础语法
SQL语句分类 DDL:数据定义语言,用来定义数据库对象:库.表.列等 DML:数据操作语言,用来定义数据库记录(数据) DCL:数据控制语言,用来定义访问权限和安全级别: DQL:数据查询语言,用来 ...
- mysql 存储过程求和_MySQL - 存储过程和函数
MySQL - 存储过程和函数 创建和调用存储过程 -- 创建stu_group()存储过程,封装 分组查询总成绩,并按照总成绩升序排序的功能 -- 注意: DELIMITER 必须写在一行的最前面, ...
- 解决mysql sum求和返回null问题或IFNULL应用
问题描述:sum求和要求返回float(或 integer或double等)类型,但当数据库不存在任何符合求和记录时,sum返回null,报类型绑定错误异常(mybatis: org.apache.i ...
- Mysql统计分组后每组数据与每组数量区别
下边统计的是分组后每组的数量:结果是多个 select p.id (count(*)) from p group by p.id 如果想统计分组后,有多个分组,需要如下执行: select count ...
- mysql数据库求和语句_MYSQL数据库SQL语句集锦
*特别说明:FILED代表数据表字段,CONDITIONS代表where之后的条件,TABLENAME代表数据表名 []中括号内的内容代表 可有可无. 创建数据库 create database ...
- mysql 相同记录求和_在MySQL中求和对应的重复记录
让我们首先创建一个表-mysql> create table DemoTable -> ( -> StudentName varchar(20), -> StudentMark ...
- Mysql统计分组区间的人数和
统计各分数区间数据 现在要统计:<50.50-60.60-70.70-80.80-90.90-100.>=100分数区间的人数: 利用 INTERVAL 划出7个区间: 再利用 elt 函 ...
- Mysql统计分组区间的人数和 1
统计各分数区间数据 现在要统计:<50.50-60.60-70.70-80.80-90.90-100.>=100分数区间的人数: 利用 INTERVAL 划出7个区间: 再利用 elt 函 ...
- mysql常量求和_Mysql之:count(*)、count(常量)、count(字段)的区别
count函数暗藏很多玄机,学习之前先尝试回答以下几个问题吧: > 1.COUNT有几种用法? > 2.COUNT(字段名)和COUNT(*)的查询结果有什么不同? > 3.COUN ...
- 【mysql 累加、累减】MySQL累积求和,累加变累减
主要内容: 1.累加求和 2.累加变累减(挪位思想) 创建表 CREATE TABLE cum_demo (id INT,money INT,PRIMARY KEY (id)); 插入数据 inser ...
最新文章
- mongodb 3.2配置内存缓存大小为MB/MongoDB 3.x内存限制配置
- Tensorflow实现MNIST数据自编码(3)
- php分页代码 页数太多,php google或baidu分页代码
- Redis精进:List的使用和应用场景
- P2030 - 【BJOI2006】狼抓兔子
- CF960G Bandit Blues 分治+NTT(第一类斯特林数)
- activiti6教程四
- 前端高效开发不得不知道的一些JavaScript库!
- 星舆科技北斗高精度定位网启动北斗三号服务
- 彻底弄懂typedef struct和struct定义结构体的区别
- 张亮:Sharding-Sphere成长记
- 转行测试,11k入职,我写了份1000多字的分享,每一笔都是经历
- iMac一体机安装苹果和Win7双系统
- 启动docker-compose时报/usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarnin
- 夏令营一部分数学试题
- 2021基于Debian的All in One(NAS+软路由)配置教程
- 我写的Javascript贪食蛇v1
- 西门子PLC控制器与ModbusTCP工业RFID读写器CK-FR08-E00的通信操作实例
- ETL和ELT的区别
- Ajax——判断用户名是否已经注册