Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
768 B
5
Indexable
Never
SELECT a.invoice_month, a.year_2011, b.year_2012, c.year_2013
FROM 

(SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
 COUNT(*) as year_2011
 FROM invoice
 WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2011
 GROUP BY invoice_date
) AS a

LEFT JOIN 

(SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
 COUNT(*) as year_2012
 FROM invoice
 WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2012
 GROUP BY invoice_date
) AS b
ON a.invoice_month = b.invoice_month

LEFT JOIN 

(SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
 COUNT(*) as year_2013
 FROM invoice
 WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2013
 GROUP BY invoice_date
) AS c
ON a.invoice_month = c.invoice_month