Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH i_month AS
- (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month
- FROM invoice
- GROUP BY invoice_month
- ORDER BY invoice_month),
- inv_2011 AS
- (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
- COUNT(invoice_id) AS year_2011
- FROM invoice
- WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2011
- GROUP BY invoice_month),
- inv_2012 AS
- (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
- COUNT(invoice_id) AS year_2012
- FROM invoice
- WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2012
- GROUP BY invoice_month),
- inv_2013 AS
- (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
- COUNT(invoice_id) AS year_2013
- FROM invoice
- WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2013
- GROUP BY invoice_month)
- SELECT i_month.invoice_month,
- inv_2011.year_2011,
- inv_2012.year_2012,
- inv_2013.year_2013
- FROM
- (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month
- FROM invoice
- GROUP BY invoice_month
- ORDER BY invoice_month) AS i_month
- LEFT JOIN
- inv_2011 ON i_month.invoice_month = inv_2011.invoice_month
- LEFT JOIN
- inv_2012 ON i_month.invoice_month = inv_2012.invoice_month
- LEFT JOIN
- inv_2013 ON i_month.invoice_month = inv_2013.invoice_month;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement