Advertisement
t7235
Oct 30th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. можно и короче
  2.  
  3. SELECT i.billing_country AS country,
  4. COUNT(i.invoice_id) AS total_invoice,
  5. tc.total_customer
  6. FROM invoice AS i
  7. LEFT JOIN (SELECT billing_country,
  8. COUNT(DISTINCT customer_id) AS total_customer
  9. FROM invoice
  10. GROUP BY billing_country) AS tc ON i.billing_country = tc.billing_country
  11. WHERE EXTRACT(YEAR FROM CAST(i.invoice_date AS timestamp)) = (
  12. SELECT EXTRACT(YEAR FROM CAST(invoice_date AS timestamp)) AS invoice_year
  13. FROM invoice
  14. WHERE EXTRACT(MONTH FROM CAST(invoice_date AS timestamp)) IN (6, 7, 8)
  15. GROUP BY invoice_year
  16. ORDER BY SUM(total) DESC
  17. LIMIT 1)
  18. GROUP BY country, total_customer
  19. ORDER BY total_invoice DESC, country;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement