ソーステーブルからIDとATTRIBUTEの数を測定し、以下の「必要なレポート」に示すようにデータを提示したいと思います。MySQLを使用しています。
ソース:
ID | DATE | ATTRIBUTE
--------------------------------
1 | 2012-01-14 | XYZ
2 | 2012-03-14 |
3 | 2012-03-15 | XYZ
4 | 2012-04-24 | ABC
5 | 2012-04-10 |
6 | 2012-05-11 | ABC
希望するレポート:
属性の数
YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
---------------------------------------------------------------------------
2010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
2011 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
2012 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
IDの数
YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
---------------------------------------------------------------------------
2010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
2011 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
2012 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
完了率(属性の数/ IDの数)
YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
---------------------------------------------------------------------------
2010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
2011 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
2012 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
これが私がこれまでに持っているコードです。ありがとう!また、データの日付フィールドから月を抽出する必要がありますが、その方法がわかりません。ありがとう。
SELECT YEAR(document_filing_date),MONTH(document_filing_date),COUNT(aif_id)
FROM (a_aif_remaining)
GROUP BY YEAR(document_filing_date),MONTH(document_filing_date);
提案された答えは機能しません!! 理由はわかりませんが、次のエラーが発生します。
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' COUNT(CASE WHEN MONTH(document_filing_date) = 1 THEN aif_id END) AS Jan, CO' at line 1"
SELECT YEAR(document_filing_date,
COUNT(CASE WHEN MONTH(document_filing_date) = 1 THEN aif_id END) AS Jan,
COUNT(CASE WHEN MONTH(document_filing_date) = 2 THEN aif_id END) AS Feb,
COUNT(CASE WHEN MONTH(document_filing_date) = 3 THEN aif_id END) AS Mar,
COUNT(CASE WHEN MONTH(document_filing_date) = 4 THEN aif_id END) AS Apr,
COUNT(CASE WHEN MONTH(document_filing_date) = 5 THEN aif_id END) AS May,
COUNT(CASE WHEN MONTH(document_filing_date) = 6 THEN aif_id END) AS Jun,
COUNT(CASE WHEN MONTH(document_filing_date) = 7 THEN aif_id END) AS Jul,
COUNT(CASE WHEN MONTH(document_filing_date) = 8 THEN aif_id END) AS Aug,
COUNT(CASE WHEN MONTH(document_filing_date) = 9 THEN aif_id END) AS Sep,
COUNT(CASE WHEN MONTH(document_filing_date) = 10 THEN aif_id END) AS Oct,
COUNT(CASE WHEN MONTH(document_filing_date) = 11 THEN aif_id END) AS Nov,
COUNT(CASE WHEN MONTH(document_filing_date) = 12 THEN aif_id END) AS Dec,
FROM a_aif_remaining
GROUP BY YEAR(document_filing_date);