例如:如下图所示,对数据库的EmailSource字段按日期分类汇总
因此我们可以写入如下的SQL语句类解决这样的问题
SELECT
sum(case when `EmailSource`='FM' then 1 else 0 end) as FM_Statistic,
sum(case when `EmailSource`='UOC' then 1 else 0 end) as UOC_Statistic,
sum(case when `EmailSource`='OC' then 1 else 0 end) as OC_Statistic,
DATE_FORMAT(Date,'%Y-%m-%d') AS `DateTime`
FROM `user_performance`
WHERE Email != '' AND Email != 'TOTAL'
AND (DATE_FORMAT(Date,'%Y-%m-%d') >= DATE_FORMAT('2011-02-5','%Y-%m-%d'))
AND (DATE_FORMAT(Date,'%Y-%m-%d') <= DATE_FORMAT('2011-03-07','%Y-%m-%d'))
GROUP BY `Date`
搜索后结果为:
FM_Statistic | UOC_Statistic | OC_Statistic | DateTime |
---|---|---|---|
1 | 0 | 1 | 2011-02-07 |
2 | 0 | 2 | 2011-02-08 |
26 | 0 | 26 | 2011-02-09 |
26 | 0 | 26 | 2011-02-10 |
28 | 0 | 28 | 2011-02-11 |
31 | 0 | 31 | 2011-02-14 |
31 | 0 | 31 | 2011-02-15 |
30 | 0 | 30 | 2011-02-16 |
29 | 0 | 29 | 2011-02-17 |
28 | 0 | 28 | 2011-02-18 |
31 | 0 | 31 | 2011-02-21 |
32 | 0 | 32 | 2011-02-22 |
30 | 0 | 30 | 2011-02-23 |
32 | 0 | 32 | 2011-02-24 |
31 | 31 | 31 | 2011-02-25 |
4 | 4 | 4 | 2011-02-26 |
5 | 5 | 5 | 2011-02-27 |
29 | 29 | 29 | 2011-02-28 |
32 | 32 | 32 | 2011-03-01 |
30 | 30 | 30 | 2011-03-02 |