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.

exer_5C27.sql 843B

123456789101112131415161718192021222324252627282930
  1. use ap;
  2. SELECT t1.vendor_state, vendor_name, t1.sum_of_invoices
  3. FROM
  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. ( ---- top invoice totals by state -----
  13. SELECT vendor_state, MAX(sum_of_invoices)
  14. AS sum_of_invoices
  15. FROM
  16. ( ---- invoice totals by vendor ----
  17. SELECT vendor_state,
  18. vendor_name,
  19. SUM(invoice_total)
  20. AS sum_of_invoices FROM vendors v JOIN invoices i
  21. ON v.vendor_id =
  22. i.vendor_id
  23. GROUP BY vendor_state,
  24. vendor_name ) t2
  25. GROUP BY vendor_state ) t3
  26. ON t1.vendor_state = t3.vendor_state AND t1.sum_of_invoices =
  27. t3.sum_of_invoices
  28. ORDER BY vendor_state