包括NULL月

时间:2016-07-07 16:12:59

标签: mysql group-by

我在获取正确的查询结果时遇到问题 我正在进行一项查询,以获取在相应月份注册了多少用户。到目前为止,我已经提出了以下查询,它允许我得到该结果,但没有显示没有用户注册的月份

SELECT 
YEAR(c.created_at) as 'YEAR',
(CASE WHEN MONTH(c.created_at)=1 THEN 'Jan' 
  WHEN (MONTH(c.created_at)=2) THEN 'Feb'
  WHEN (MONTH(c.created_at)=3) THEN 'Mar'
  WHEN (MONTH(c.created_at)=4) THEN 'Apr'
  WHEN (MONTH(c.created_at)=5) THEN 'May'
  WHEN (MONTH(c.created_at)=6) THEN 'Jun'
  WHEN (MONTH(c.created_at)=7) THEN 'Jul'
  WHEN (MONTH(c.created_at)=8) THEN 'Aug'
  WHEN (MONTH(c.created_at)=9) THEN 'Sep'
  WHEN (MONTH(c.created_at)=10) THEN 'Oct' 
  WHEN (MONTH(c.created_at)=11) THEN 'Nov'
  WHEN (MONTH(c.created_at)=12) THEN 'Dec'
END)
 as 'MONTH',
COUNT(c.id) as Total
FROM customers as c
WHERE YEAR(c.created_at) = 2016
GROUP BY c.id, MONTH(c.created_at)
ORDER BY YEAR(c.created_at), MONTH(c.created_at), Total ASC


结果:

+------+-------+-------+
| YEAR | MONTH | Total |
+------+-------+-------+
| 2016 | Feb   |     1 |
| 2016 | Apr   |     1 |
| 2016 | May   |     1 |
| 2016 | Jul   |     1 |
+------+-------+-------+
4 rows in set (0.00 sec)

我希望它像这样

+------+-------+-------+
| YEAR | MONTH | Total |
+------+-------+-------+
| 2016 | Jan   |     0 |
| 2016 | Feb   |     1 |
| 2016 | Mar   |     0 |
| 2016 | Apr   |     1 |
| 2016 | May   |     1 |
| 2016 | Jun   |     0 |
| 2016 | Jul   |     1 |
| 2016 | Aug   |     0 |
| 2016 | Sep   |     0 |
| 2016 | Oct   |     0 |
| 2016 | Nov   |     0 |
| 2016 | Dec   |     0 |
+------+-------+-------+

3 个答案:

答案 0 :(得分:0)

试试这个

使用SUM代替COUNT。无论如何你要显示零或一。

 SELECT 
    YEAR(c.created_at) as 'YEAR',
    (CASE WHEN MONTH(c.created_at)=1 THEN 'Jan' 
      WHEN (MONTH(c.created_at)=2) THEN 'Feb'
      WHEN (MONTH(c.created_at)=3) THEN 'Mar'
      WHEN (MONTH(c.created_at)=4) THEN 'Apr'
      WHEN (MONTH(c.created_at)=5) THEN 'May'
      WHEN (MONTH(c.created_at)=6) THEN 'Jun'
      WHEN (MONTH(c.created_at)=7) THEN 'Jul'
      WHEN (MONTH(c.created_at)=8) THEN 'Aug'
      WHEN (MONTH(c.created_at)=9) THEN 'Sep'
      WHEN (MONTH(c.created_at)=10) THEN 'Oct' 
      WHEN (MONTH(c.created_at)=11) THEN 'Nov'
      WHEN (MONTH(c.created_at)=12) THEN 'Dec'
    END) AS 'MONTH',
    SUM (CASE WHEN c.id IS NOT NULL THEN 1 ELSE 0 END ) as Total
 FROM customers as c
 WHERE YEAR(c.created_at) = 2016
 GROUP BY c.id, MONTH(c.created_at)
 ORDER BY YEAR(c.created_at), MONTH(c.created_at), Total ASC

答案 1 :(得分:0)

你可以创建一个12行表到外连接

答案 2 :(得分:0)

我已经提出了这个解决方案,我的情况很好 我使用以下查询

为这几个月创建了一个关键表
SELECT 
YEAR(b.created_at) as 'YEAR',
COUNT(CASE WHEN MONTH(b.created_at)=1 THEN b.id END) as Jan,
COUNT(CASE WHEN MONTH(b.created_at)=2 THEN b.id END) as Feb,
COUNT(CASE WHEN MONTH(b.created_at)=3 THEN b.id END) as Mar,
COUNT(CASE WHEN MONTH(b.created_at)=4 THEN b.id END) as Apr,
COUNT(CASE WHEN MONTH(b.created_at)=5 THEN b.id END) as May,
COUNT(CASE WHEN MONTH(b.created_at)=6 THEN b.id END) as Jun,
COUNT(CASE WHEN MONTH(b.created_at)=7 THEN b.id END) as Jul,
COUNT(CASE WHEN MONTH(b.created_at)=8 THEN b.id END) as Aug,
COUNT(CASE WHEN MONTH(b.created_at)=9 THEN b.id END) as Sep,
COUNT(CASE WHEN MONTH(b.created_at)=10 THEN b.id END) as Oct,
COUNT(CASE WHEN MONTH(b.created_at)=11 THEN b.id END) as Nov,
COUNT(CASE WHEN MONTH(b.created_at)=12 THEN b.id END) as 'Dec',
COUNT(b.id) as Total
FROM customers as b 
GROUP BY YEAR(b.created_at)
ORDER BY YEAR(b.created_at) ASC
产生此输出的

+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
| YEAR | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Total |
+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
| 2015 |   0 |   0 |   0 |   0 |   0 |   0 |   1 |   0 |   0 |   0 |   1 |   0 |     2 |
| 2016 |   0 |   1 |   0 |   1 |   1 |   0 |   1 |   0 |   0 |   0 |   0 |   0 |     4 |
+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
2 rows in set (0.00 sec)

从技术上讲,它解决了这个问题,因为它包括空月。