2017-08-29 17 views
2

このクエリを取得して1行にカウントを返すのが難しいです。 結果を示す1行異なる年とは、トランザクションが(この場合は2016のように)がない場合IFNULLクエリが2行同じ結果を返す

私は、このコードを実行異なる年間でトランザクションを挿入し、結果は同じ結果と2行で

を示しますここで私は私のプロジェクトで毎年報告書を作成しようとしている

select 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=1)AND (YEAR(date)='2017'))),0) AS `Jan`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=2)AND (YEAR(date)='2017'))),0) AS `Feb`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=3)AND (YEAR(date)='2017'))),0) AS `Mar`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=4)AND (YEAR(date)='2017'))),0) AS `Apr`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=5)AND (YEAR(date)='2017'))),0) AS `May`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=6)AND (YEAR(date)='2017'))),0) AS `Jun`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=7)AND (YEAR(date)='2017'))),0) AS `Jul`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=8)AND (YEAR(date)='2017'))),0) AS `Aug`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=9)AND (YEAR(date)='2017'))),0) AS `Sep`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=10)AND (YEAR(date)='2017'))),0) AS `Oct`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=11)AND (YEAR(date)='2017'))),0) AS `Nov`, 
      ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=12)AND (YEAR(date)='2017'))),0) AS `Dec` 
     from transaction GROUP BY YEAR(date) 

コードだ、このまたはそれが動作することができます別のクエリを修正する方法はありますか?

答えて

1

あなたがそれぞれにそうでなければ、どこ

select 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=1)AND (YEAR(date)='2017'))),0) AS `Jan`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=2)AND (YEAR(date)='2017'))),0) AS `Feb`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=3)AND (YEAR(date)='2017'))),0) AS `Mar`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=4)AND (YEAR(date)='2017'))),0) AS `Apr`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=5)AND (YEAR(date)='2017'))),0) AS `May`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=6)AND (YEAR(date)='2017'))),0) AS `Jun`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=7)AND (YEAR(date)='2017'))),0) AS `Jul`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=8)AND (YEAR(date)='2017'))),0) AS `Aug`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=9)AND (YEAR(date)='2017'))),0) AS `Sep`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=10)AND (YEAR(date)='2017'))),0) AS `Oct`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=11)AND (YEAR(date)='2017'))),0) AS `Nov`, 
     ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=12)AND (YEAR(date)='2017'))),0) AS `Dec` 
    from transaction 
    where YEAR(date)='2017' 
    GROUP BY YEAR(date) 

で、このためにフィルタリングする必要があります2017年のデータのみが必要な場合はあなたがあなたのデータにIFNULL(2017年に関連する)で同じ選択が行われており、返します同じ値

あなたは(役に立たない避けることができ)と副選択例:

select 
     sum(case when month(date)=1 and YEAR(date) = '2017' then 1 else 0 end) as 'Jan' 
     sum(case when month(date)=2 and YEAR(date) = '2017' then 1 else 0 end) as 'Feb' 
     ...... 
     sum(case when month(date)=12 and YEAR(date) = '2017' then 1 else 0 end) as 'Dec' 
from transaction 
    where YEAR(date)='2017' 
    GROUP BY YEAR(date) 
1
select 
SUM(Month(date)=1) AND (YEAR(date)='2017') AS `Jan`, 
SUM(Month(date)=2) AND (YEAR(date)='2017') AS `Feb`, 
SUM(Month(date)=3) AND (YEAR(date)='2017') AS `Mar`, 
SUM(Month(date)=4) AND (YEAR(date)='2017') AS `Apr`, 
SUM(Month(date)=5) AND (YEAR(date)='2017') AS `May`, 
SUM(Month(date)=6) AND (YEAR(date)='2017') AS `Jun`, 
SUM(Month(date)=7) AND (YEAR(date)='2017') AS `Jul`, 
SUM(Month(date)=8) AND (YEAR(date)='2017') AS `Aug`, 
SUM(Month(date)=9) AND (YEAR(date)='2017') AS `Sep`, 
SUM(Month(date)=10) AND (YEAR(date)='2017') AS `Oct`, 
SUM(Month(date)=11) AND (YEAR(date)='2017') AS `Nov`, 
SUM(Month(date)=12) AND (YEAR(date)='2017') AS `Dec` 
from transaction 
GROUP BY YEAR(date) 

上記のコードを試すことができます。

ここに私は単純に条件を追加しましたSUM機能。 これがMonth(date)=1) AND (YEAR(date)='2017'の場合、それは1を返します。それ以外の場合は0を返します。

最終的に期待される出力が得られます。

お手伝いします。

1

以下は一般的なクエリです。

SELECT COUNT(id), YEAR(created) as year, MONTH(created) as mon 
FROM transaction 
WHERE date IS NOT NULL 
GROUP BY YEAR(date), MONTH(date) Desc 

あなたが見つけた年月のすべての月に結果が表示され、コードでは行方不明のものが表示されます。