Advertisement
Step8rother

Временные таблицы, задача 2

Jun 29th, 2023
1,964
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.15 KB | Source Code | 0 0
  1. /*Перепишите один из своих прошлых запросов, используя оператор WITH.
  2. Составьте сводную таблицу. Посчитайте заказы, оформленные за каждый месяц в течение нескольких лет: с 2011 по 2013 год. Итоговая таблица должна включать четыре поля: invoice_month, year_2011, year_2012, year_2013. Поле month должно хранить месяц в виде числа от 1 до 12.
  3. Если в какой-либо месяц заказы не оформляли, номер такого месяца всё равно должен попасть в таблицу.
  4. */
  5.  
  6. --P.S переписывать ничего не стал. вот стану мидлом, буду заниматься рефракторингом, а пока действую по принципу "работает - не трогай"
  7. SELECT i_month.invoice_month,
  8.        inv_2011.year_2011,
  9.        inv_2012.year_2012,
  10.        inv_2013.year_2013
  11. FROM
  12.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month
  13.    FROM invoice
  14.    GROUP BY invoice_month
  15.    ORDER BY invoice_month) AS i_month
  16.    
  17.    
  18. LEFT JOIN
  19.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
  20.           COUNT(invoice_id) AS year_2011
  21.    FROM invoice
  22.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2011
  23.    GROUP BY invoice_month) AS inv_2011 ON i_month.invoice_month = inv_2011.invoice_month
  24.    
  25.    
  26. LEFT JOIN
  27.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
  28.           COUNT(invoice_id) AS year_2012
  29.    FROM invoice
  30.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2012
  31.    GROUP BY invoice_month) AS inv_2012 ON i_month.invoice_month = inv_2012.invoice_month
  32.    
  33.    
  34. LEFT JOIN
  35.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
  36.           COUNT(invoice_id) AS year_2013
  37.    FROM invoice
  38.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2013
  39.    GROUP BY invoice_month) AS inv_2013 ON i_month.invoice_month = inv_2013.invoice_month;
Advertisement
Comments
  • FillComp
    1 year
    1. WITH i_month AS
    2.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month
    3.    FROM invoice
    4.    GROUP BY invoice_month
    5.    ORDER BY invoice_month),
    6. inv_2011 AS
    7.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
    8.           COUNT(invoice_id) AS year_2011
    9.    FROM invoice
    10.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2011
    11.    GROUP BY invoice_month),
    12. inv_2012 AS
    13.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
    14.           COUNT(invoice_id) AS year_2012
    15.    FROM invoice
    16.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2012
    17.    GROUP BY invoice_month),
    18. inv_2013 AS
    19.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month,
    20.           COUNT(invoice_id) AS year_2013
    21.    FROM invoice
    22.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2013
    23.    GROUP BY invoice_month)
    24.  
    25.    
    26. SELECT i_month.invoice_month,
    27.        inv_2011.year_2011,
    28.        inv_2012.year_2012,
    29.        inv_2013.year_2013
    30. FROM
    31.   (SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS invoice_month
    32.    FROM invoice
    33.    GROUP BY invoice_month
    34.    ORDER BY invoice_month) AS i_month
    35.    
    36. LEFT JOIN
    37.   inv_2011 ON i_month.invoice_month = inv_2011.invoice_month
    38.    
    39. LEFT JOIN
    40.   inv_2012 ON i_month.invoice_month = inv_2012.invoice_month
    41.    
    42. LEFT JOIN
    43.   inv_2013 ON i_month.invoice_month = inv_2013.invoice_month;
Add Comment
Please, Sign In to add comment
Advertisement