You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

demo-queries-from slides-11.sql 1000B

1234567891011121314151617181920212223242526272829
  1. SELECT t1.vendor_state, vendor_name, t1.sum_of_invoices
  2. FROM
  3. (
  4. -- invoice totals by vendor
  5. SELECT vendor_state, vendor_name,
  6. SUM(invoice_total) AS sum_of_invoices
  7. FROM vendors v JOIN invoices i
  8. ON v.vendor_id = i.vendor_id
  9. GROUP BY vendor_state, vendor_name
  10. ) t1
  11. JOIN
  12. (
  13. -- top invoice totals by state
  14. SELECT vendor_state,
  15. MAX(sum_of_invoices) AS sum_of_invoices
  16. FROM
  17. (
  18. -- invoice totals by vendor
  19. SELECT vendor_state, vendor_name,
  20. SUM(invoice_total) AS sum_of_invoices
  21. FROM vendors v JOIN invoices i
  22. ON v.vendor_id = i.vendor_id
  23. GROUP BY vendor_state, vendor_name
  24. ) t2
  25. GROUP BY vendor_state
  26. ) t3
  27. ON t1.vendor_state = t3.vendor_state AND
  28. t1.sum_of_invoices = t3.sum_of_invoices
  29. ORDER BY vendor_state;