Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Составьте сводную таблицу. Посчитайте заказы, оформленные за каждый месяц в течение нескольких лет: с 2011 по 2013 год. Итоговая таблица должна включать четыре поля: invoice_month, year_2011, year_2012, year_2013. Поле invoice_month должно хранить месяц в виде числа от 1 до 12.
- Если в какой-либо месяц заказы не оформляли, номер такого месяца всё равно должен попасть в таблицу.
- В этом задании не будет подсказок. Используйте любые методы, которые посчитаете нужными.
- */
- 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
- (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) AS inv_2011 ON i_month.invoice_month = inv_2011.invoice_month
- LEFT JOIN
- (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) AS inv_2012 ON i_month.invoice_month = inv_2012.invoice_month
- LEFT JOIN
- (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) AS inv_2013 ON i_month.invoice_month = inv_2013.invoice_month
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement