Mysql montly两年之间的总和?

时间:2017-06-05 10:48:36

标签: mysql datetime sum pivot-table aggregate-functions

我正在获取特定年份的周记录。

   SELECT ....
      SUM(IF(YEARWEEK(readdate) = 201602, readamount, 0)) AS "201601w",
      SUM(IF(YEARWEEK(readdate) = 201701, readamount, 0)) AS "201701w" 
    FROM ...

如何在2到3年之间获得每月?

例如:

   SELECT ....
      SUM(IF(YEARMONTH(readdate) = 201602(Feb), readamount, 0)) AS "201601m",
      SUM(IF(YEARMONTH(readdate) = 201701(Jan), readamount, 0)) AS "201701m" 
    FROM ...

我的完整查询:

SELECT wirters.writer,
SUM(IF(YEARWEEK(readdate) = 201602, read, 0)) AS "201601",
SUM(IF(YEARWEEK(readdate) = 201701, read, 0)) AS "201701" 
FROM writers
LEFT JOIN reads ON wirters.writer_id = reads.writer_id   
GROUP BY wirters.writer

我需要输出

+---------------+---------+---------+
| writer        | 2016Feb | 2017Jan | 
+---------------+---------+---------+
| George Orwell |    13   |   663   |
| Dan Brown     |    22   |   32    |
+---------------+---------+---------+

0 个答案:

没有答案