赞
踩
在mysql中如何实现1-12月份数据的统计查询?
首先我的数据表大概是这样的:
查询sql如下:
SELECT PUR_BILL_TO_NAME AS "公司",DEPT_NAME AS "部门",
sum( CASE month(T_TIME) WHEN '1' THEN AUD_P_COUNT ELSE 0 END) AS "一月",
sum(CASE month(T_TIME) WHEN '2' THEN AUD_P_COUNT ELSE 0 END ) AS "二月",
sum(CASE month(T_TIME) WHEN '3' THEN AUD_P_COUNT ELSE 0 END) AS "三月",
sum( CASE month(T_TIME) WHEN '4' THEN AUD_P_COUNT ELSE 0 END) AS "四月",
sum(CASE month(T_TIME) WHEN '5' THEN AUD_P_COUNT ELSE 0 END) AS "五月",
sum( CASE month(T_TIME) WHEN '6' THEN AUD_P_COUNT ELSE 0 END) AS "六月",
sum( CASE month(T_TIME) WHEN '7' THEN AUD_P_COUNT ELSE 0 END) AS "七月",
sum( CASE month(T_TIME) WHEN '8' THEN AUD_P_COUNT ELSE 0 END ) AS "八月",
sum( CASE month(T_TIME) WHEN '9' THEN AUD_P_COUNT ELSE 0 END ) AS "九月",
sum( CASE month(T_TIME) WHEN '10' THEN AUD_P_COUNT ELSE 0 END ) AS "十月",
sum( CASE month(T_TIME) WHEN '11' THEN AUD_P_COUNT ELSE 0 END ) AS "十一月",
sum( CASE month(T_TIME) WHEN '12' THEN AUD_P_COUNT ELSE 0 END ) AS "十二月",
sum( CASE WHEN 1=1 THEN AUD_P_COUNT ELSE 0 END ) AS "累计"
FROM
T_DWS_SRV_CGDM001
GROUP BY PUR_BILL_TO_NAME,
DEPT_NAME ;
效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。