mysql一条sql查出多个条件不同的sum或count问题
一条sql查出多个条件不同的sum或count
最近做java后台遇到很多问题记录一下,有个需求是在一条sql中同时统计到几条条件不一样的sql或count,就用了子查询,希望能帮助到需要的朋友,上代码和效果图:
select sum(pzgwjscd) as allJsgm,sbjh.nd as nd, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3301') and ptgw.zt != 0 and sbjh.nd=2017 ) as hzJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3303') and ptgw.zt != 0 and sbjh.nd=2017) as wzJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3304') and ptgw.zt != 0 and sbjh.nd=2017 ) as jxJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3305') and ptgw.zt != 0 and sbjh.nd=2017) as huzJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3306') and ptgw.zt != 0 and sbjh.nd=2017 ) as sxJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3307') and ptgw.zt != 0 and sbjh.nd=2017 ) as jhJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3308') and ptgw.zt != 0 and sbjh.nd=2017 ) as qzJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3309') and ptgw.zt != 0 and sbjh.nd=2017 ) as zsJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3310') and ptgw.zt != 0 and sbjh.nd=2017 ) as tzJsgm, ( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3311') and ptgw.zt != 0 and sbjh.nd=2017 ) as lsJsgm from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id where 1=1 and ptgw.zt != 0 and sbjh.nd=2017
数据库表
查询结果:
Sql根据不同条件统计总数
经常会遇到根据不同的条件统计总数的问题,一般有两种写法:count和sum都可以
数据准备:
方法一 :Count
代码:
SELECT COUNT( CASE WHEN age > 20 AND age < 25 THEN 1 ELSE NULL END ) AS cnt0, COUNT( CASE WHEN age >= 25 AND age < 30 THEN 1 ELSE NULL END ) AS cnt1 FROM USER;
结果:
方法二:sum
代码:
SELECT SUM( CASE WHEN age > 20 AND age < 25 THEN 1 ELSE 0 END ) AS cnt0, SUM( CASE WHEN age >= 25 AND age < 30 THEN 1 ELSE 0 END ) AS cnt1 FROM USER;
结果:
当然也可以和count代码一样ELSE后面也写为NULL
SELECT SUM( CASE WHEN age > 20 AND age < 25 THEN 1 ELSE NULL END ) AS cnt0, SUM( CASE WHEN age >= 25 AND age < 30 THEN 1 ELSE NULL END ) AS cnt1 FROM USER;
后记
其实原理很简单,count统计的时候有满足条件的就加1,没有满足的变为NULL,我们知道聚合函数统计的时候是忽略null值的;而sum原理和coun相似,不过ELSE后面可以是0或者NULL。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
linux系统下实现mysql热备份详细步骤(mysql主从复制)
这篇文章主要介绍了linux系统下实现MySQL主从热备份2013-12-12MySQL数据库show processlist指令使用解析
这篇文章主要介绍了MySQL数据库show processlist指令使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-11-11MySQL中datetime和timestamp的区别及使用详解
这篇文章主要介绍了MySQL中datetime和timestamp的区别及使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-11-11CentOS7下MySQL5.7安装配置方法图文教程(YUM)
这篇文章主要为大家详细介绍了CentOS7下MySQL5.7安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-01-01
最新评论