Advertisement
Step8rother

Подзапросы в FROM, задача 5

Jun 27th, 2023
2,828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.96 KB | Source Code | 0 0
  1. --Для каждой страны посчитайте среднюю стоимость заказов в 2009 году по месяцам. Отберите данные за 2, 5, 7 и 10 месяцы и сложите средние значения стоимости заказов. Выведите названия стран, у которых это число превышает 10 долларов.
  2.  
  3. SELECT sub.country
  4. FROM
  5.   (SELECT billing_country AS country,
  6.           EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) AS YEAR,
  7.           EXTRACT(MONTH FROM CAST (invoice_date AS DATE)) AS MONTH,
  8.           AVG(total) AS avg_total
  9.    FROM invoice AS i
  10.    WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) = 2009
  11.    GROUP BY billing_country,
  12.             EXTRACT(YEAR FROM CAST(invoice_date AS DATE)),
  13.             EXTRACT (MONTH FROM CAST(invoice_date AS DATE))) AS sub
  14. WHERE sub.MONTH IN (2, 5, 7, 10)
  15. GROUP BY sub.country
  16. HAVING SUM(sub.avg_total) > 10;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement