数据库
首页 > 数据库> > MySQL行列转换与聚合函数

MySQL行列转换与聚合函数

作者:互联网

一、数据结构
在这里插入图片描述
二、需要结果:
在这里插入图片描述
三、MySQL行列转换与聚合函数

SELECT IFNULL(ADMDIVNAME, 'TATOLSUM') AS ADMDIVNAME,
	SUM(IF(STAFF_MANAGE.ORGANTYPECODE ='SYDW', STAFFTOTAL, 0)) AS SYDW_BZ_RS,
	SUM(IF(STAFF_MANAGE.ORGANTYPECODE ='XZJG', STAFFTOTAL, 0)) AS XZJG_BZ_RS,
	SUM(IF(STAFF_MANAGE.ORGANTYPECODE ='SYDW', JOBTOTAL, 0)) AS SYDW_JOB_RS,
	SUM(IF(STAFF_MANAGE.ORGANTYPECODE ='XZJG', JOBTOTAL, 0)) AS XZJG_JOB_RS,
	COUNT(DISTINCT (CASE WHEN STAFF_MANAGE.ORGANTYPECODE ='SYDW' THEN JOBTOTAL END)) AS SYDW_NUM,
	COUNT(DISTINCT (CASE WHEN STAFF_MANAGE.ORGANTYPECODE ='XZJG' THEN JOBTOTAL END)) AS XZJG_NUM
FROM STAFF_MANAGE
GROUP BY ADMDIVNAME
WITH ROLLUP;

抽时间记录,大家共同学习。
https://www.cnblogs.com/weibanggang/p/9679301.html
https://blog.csdn.net/weixin_40683253/article/details/80814085

标签:聚合,XZJG,SUM,MANAGE,行列,ORGANTYPECODE,MySQL,SYDW,STAFF
来源: https://blog.csdn.net/qq_21442135/article/details/123075227