2016-07-07 11 views
1

クエリの正しい結果を得るのに問題があります
それぞれの月に登録されているユーザー数を取得するためのクエリを作成しています。
NULL月を含める

+------+-------+-------+ 
| YEAR | MONTH | Total | 
+------+-------+-------+ 
| 2016 | Feb |  1 | 
| 2016 | Apr |  1 | 
| 2016 | May |  1 | 
| 2016 | Jul |  1 | 
+------+-------+-------+ 
4 rows in set (0.00 sec) 
:これまでのところ私は、私はその結果を取得することができますが、何のユーザーが

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 | 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 | 
+------+-------+-------+ 
のようになりたい

答えて

0

このようにしてください

COUNTの代わりにSUMを使用してください。とにかく、ゼロまたは1を表示しようとしています。

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 
0

あなたは私が私の状況でうまく動作このソリューション私が出ているアウターが

0

に対して参加するために12行の表を作成することができます。
私はそれがヌルヶ月が含まれているため、それが問題を解決し

+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+ 
| 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) 

技術的には、この出力を生成し、次のクエリ

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 

を使っヶ月間極めて重要なテーブルを作成しました。

関連する問題