Advertisement
Vitaliy_Novichikhin

SQL 1/4 3. Как сочетать объединения и подзапросы

Jun 24th, 2022
4,869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.95 KB | None | 0 0
  1. SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS MONTH,
  2.        COUNT(invoice_id) AS year_2011
  3. FROM invoice
  4. WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2011
  5. --OR EXTRACT(YEAR FROM CAST(invoice_date AS date)) = 2012
  6. --OR EXTRACT(YEAR FROM CAST(invoice_date AS date)) = 2013
  7. GROUP BY MONTH
  8.  
  9. UNION ALL
  10.  
  11. SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS MONTH,
  12.        COUNT(invoice_id) AS year_2012
  13. FROM invoice
  14. --WHERE EXTRACT(YEAR FROM CAST(invoice_date AS date)) = 2011
  15. WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2012
  16. --OR EXTRACT(YEAR FROM CAST(invoice_date AS date)) = 2013
  17. GROUP BY MONTH
  18.  
  19. UNION ALL
  20.  
  21. SELECT EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) AS MONTH,
  22.        COUNT(invoice_id) AS year_2013
  23. FROM invoice
  24. --WHERE EXTRACT(YEAR FROM CAST(invoice_date AS date)) = 2011
  25. --WHERE EXTRACT(YEAR FROM CAST(invoice_date AS date)) = 2012
  26. WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2013
  27. GROUP BY MONTH
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement