Advertisement
This is comment for paste
Как сочетать объединения и подзапросы, задача 4
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- можно и короче
- SELECT i.billing_country AS country,
- COUNT(i.invoice_id) AS total_invoice,
- tc.total_customer
- FROM invoice AS i
- LEFT JOIN (SELECT billing_country,
- COUNT(DISTINCT customer_id) AS total_customer
- FROM invoice
- GROUP BY billing_country) AS tc ON i.billing_country = tc.billing_country
- WHERE EXTRACT(YEAR FROM CAST(i.invoice_date AS timestamp)) = (
- SELECT EXTRACT(YEAR FROM CAST(invoice_date AS timestamp)) AS invoice_year
- FROM invoice
- WHERE EXTRACT(MONTH FROM CAST(invoice_date AS timestamp)) IN (6, 7, 8)
- GROUP BY invoice_year
- ORDER BY SUM(total) DESC
- LIMIT 1)
- GROUP BY country, total_customer
- ORDER BY total_invoice DESC, country;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement