以订单统计为例,前端展示柱状图(Jquery统计):
表及主要字段描述如下;表名:Orders1.日期CreateTime2.金额Amount3.用户UserID
情况一:根据部门统计某一年每月销量(查询一个部门月统计)
1)直接在SQL语句中判断每月信息,好处,前台直接调用;坏处,性能不高。
SQL语句:
SELECT SUM(CASE WHEN MONTH(s.CreateTime) = 1 THEN s.Amount ELSE 0 END) AS '一月',SUM(CASE WHEN MONTH(s.CreateTime) = 2 THEN s.Amount ELSE 0 END) AS '二月',SUM(CASE WHEN MONTH(s.CreateTime) = 3 THEN s.Amount ELSE 0 END) AS '三月',SUM(CASE WHEN MONTH(s.CreateTime) = 4 THEN s.Amount ELSE 0 END) AS '四月',SUM(CASE WHEN MONTH(s.CreateTime) = 5 THEN s.Amount ELSE 0 END) AS '五月',SUM(CASE WHEN MONTH(s.CreateTime) = 6 THEN s.Amount ELSE 0 END) AS '六月',SUM(CASE WHEN MONTH(s.CreateTime) = 7 THEN s.Amount ELSE 0 END) AS '七月',SUM(CASE WHEN MONTH(s.CreateTime) = 8 THEN s.Amount ELSE 0 END) AS '八月',SUM(CASE WHEN MONTH(s.CreateTime) = 9 THEN s.Amount ELSE 0 END) AS '九月',SUM(CASE WHEN MONTH(s.CreateTime) = 10 THEN s.Amount ELSE 0 END) AS '十月',SUM(CASE WHEN MONTH(s.CreateTime) = 11 THEN s.Amount ELSE 0 END) AS '十一月',SUM(CASE WHEN MONTH(s.CreateTime) = 12 THEN s.Amount ELSE 0 END) AS '十二月'FROM Orders AS sWHERE YEAR(s.CreateTime) = 2014
--其他条件
结果:
一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 741327.00 120505.00 0.00
2)统计出数据库里有值的月份,再前端逻辑判断其他月份补0
SQL语句:
SELECTUserID,MONTH ( CreateTime ) as 月份,SUM( Amount ) as 统计FROMOrdersWHEREYEAR ( CreateTime ) = 2014 -- 这里假设你要查 2014年的每月的统计。--其他条件GROUP BYUserID, MONTH ( CreateTime )结果:月份 销售额10 741327.0011 120505.00
情况二:统计所有部门某一年每月销量
1)此数据量大的话影响性能,SQL语句(这里未联查部门表):
SELECT UserID,SUM(CASE WHEN MONTH(s.CreateTime) = 1 THEN s.Amount ELSE 0 END) AS '一月',SUM(CASE WHEN MONTH(s.CreateTime) = 2 THEN s.Amount ELSE 0 END) AS '二月',SUM(CASE WHEN MONTH(s.CreateTime) = 3 THEN s.Amount ELSE 0 END) AS '三月',SUM(CASE WHEN MONTH(s.CreateTime) = 4 THEN s.Amount ELSE 0 END) AS '四月',SUM(CASE WHEN MONTH(s.CreateTime) = 5 THEN s.Amount ELSE 0 END) AS '五月',SUM(CASE WHEN MONTH(s.CreateTime) = 6 THEN s.Amount ELSE 0 END) AS '六月',SUM(CASE WHEN MONTH(s.CreateTime) = 7 THEN s.Amount ELSE 0 END) AS '七月',SUM(CASE WHEN MONTH(s.CreateTime) = 8 THEN s.Amount ELSE 0 END) AS '八月',SUM(CASE WHEN MONTH(s.CreateTime) = 9 THEN s.Amount ELSE 0 END) AS '九月',SUM(CASE WHEN MONTH(s.CreateTime) = 10 THEN s.Amount ELSE 0 END) AS '十月',SUM(CASE WHEN MONTH(s.CreateTime) = 11 THEN s.Amount ELSE 0 END) AS '十一月',SUM(CASE WHEN MONTH(s.CreateTime) = 12 THEN s.Amount ELSE 0 END) AS '十二月'FROM Orders AS sWHERE YEAR(s.CreateTime) = 2014 group by UserID
结果:
UserID 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 53495.00 0.002 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 738862.00 37968.00 0.003 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2099.00 22849.00 0.004 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 366.00 0.00 0.005 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 6193.00 0.00
2)百度看到有人提到列转行,未看到实例,不太清楚具体实现方式。有知道的朋友,请告知,谢谢!
SELECTUserID,MONTH ( CreateTime ) as 月份,SUM( Amount ) as 统计FROMOrdersWHEREYEAR ( CreateTime ) = 2014 -- 这里假设你要查 2014年的每月的统计。GROUP BYUserID,MONTH ( CreateTime )结果:UserID 月份 统计1 10 738862.002 10 2099.003 10 366.004 11 53495.001 11 37968.002 11 22849.00
5 11 6193.00
新闻热点
疑难解答