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.

exercise1-9.sql 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. USE ap;
  2. -- Exercise 1 --
  3. SELECT DISTINCT
  4. v.vendor_id,
  5. SUM(i.invoice_total) as 'Total of Invoices'
  6. FROM vendors v, invoices i
  7. GROUP BY i.vendor_id;
  8. -- Exercise 2 --
  9. SELECT
  10. vendor_name, SUM(payment_total) AS 'Payment Total Sum'
  11. FROM vendors v INNER JOIN invoices i
  12. ON v.vendor_id = i.vendor_id
  13. GROUP BY v.vendor_name
  14. ORDER BY SUM(i.payment_total) DESC;
  15. -- Opletten met SUM in select voor verkeerde data, order by (bijna) altijd met Agg function--
  16. -- Exercise 3--
  17. SELECT
  18. vendor_name,
  19. COUNT(invoice_total) AS'Number of Invoices',
  20. SUM(invoice_total) AS 'Invoice Total'
  21. FROM
  22. vendors v INNER JOIN invoices i
  23. ON v.vendor_id = i.vendor_id
  24. GROUP BY v.vendor_name
  25. ORDER BY COUNT(invoice_total) DESC;
  26. -- Exercise 4 --
  27. SELECT
  28. g.account_description,
  29. COUNT(line_item_amount) AS 'Number of Items',
  30. SUM(line_item_amount) AS 'Total of Items'
  31. FROM
  32. general_ledger_accounts g INNER JOIN invoice_line_items il
  33. ON g.account_number = il.account_number
  34. GROUP BY g.account_description
  35. HAVING COUNT(line_item_amount) > 1
  36. ORDER BY SUM(line_item_amount) DESC;
  37. -- Exercise 5--
  38. SELECT
  39. g.account_description,
  40. COUNT(line_item_amount) AS 'Number of Items',
  41. SUM(line_item_amount) AS 'Total of Items',
  42. invoice_date
  43. FROM general_ledger_accounts g INNER JOIN invoice_line_items il
  44. ON g.account_number = il.account_number
  45. INNER JOIN invoices i
  46. ON i.invoice_id = il.invoice_id
  47. GROUP BY g.account_number
  48. HAVING count(il.line_item_amount) > 1
  49. AND invoice_date >= '2018-04-01' AND invoice_date <='2018-06-30' -- beter in de where plaatsen voor datasnelheid--
  50. ORDER BY SUM(line_item_amount) DESC;
  51. -- Exercise 6 --
  52. SELECT
  53. account_number,
  54. SUM(line_item_amount)
  55. FROM invoice_line_items il
  56. GROUP BY account_number
  57. WITH ROLLUP;
  58. -- Exercise 7 --
  59. SELECT
  60. vendor_name,
  61. COUNT( DISTINCT ili.account_number)
  62. FROM
  63. vendors v INNER JOIN invoices i
  64. ON v.vendor_id = i.vendor_id
  65. INNER JOIN invoice_line_items ili
  66. ON i.invoice_id = ili.invoice_id
  67. GROUP BY vendor_name
  68. HAVING COUNT( DISTINCT ili.account_number) > 1;