Queer European MD passionate about IT

600Solutions.sql 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Screen 3 */
  2. -- Table descriptions
  3. SELECT 'Customers' AS table_name,
  4. 13 AS number_of_attribute,
  5. COUNT(*) AS number_of_row
  6. FROM Customers
  7. UNION ALL
  8. SELECT 'Products' AS table_name,
  9. 9 AS number_of_attribute,
  10. COUNT(*) AS number_of_row
  11. FROM Products
  12. UNION ALL
  13. SELECT 'ProductLines' AS table_name,
  14. 4 AS number_of_attribute,
  15. COUNT(*) AS number_of_row
  16. FROM ProductLines
  17. UNION ALL
  18. SELECT 'Orders' AS table_name,
  19. 7 AS number_of_attribute,
  20. COUNT(*) AS number_of_row
  21. FROM Orders
  22. UNION ALL
  23. SELECT 'OrderDetails' AS table_name,
  24. 5 AS number_of_attribute,
  25. COUNT(*) AS number_of_row
  26. FROM OrderDetails
  27. UNION ALL
  28. SELECT 'Payments' AS table_name,
  29. 4 AS number_of_attribute,
  30. COUNT(*) AS number_of_row
  31. FROM Payments
  32. UNION ALL
  33. SELECT 'Employees' AS table_name,
  34. 8 AS number_of_attribute,
  35. COUNT(*) AS number_of_row
  36. FROM Employees
  37. UNION ALL
  38. SELECT 'Offices' AS table_name,
  39. 9 AS number_of_attribute,
  40. COUNT(*) AS number_of_row
  41. FROM Offices;
  42. /* Screen 4 */
  43. --Low stock
  44. SELECT productCode,
  45. ROUND(SUM(quantityOrdered) * 1.0 / (SELECT quantityInStock
  46. FROM products p
  47. WHERE od.productCode = p.productCode), 2) AS low_stock
  48. FROM orderdetails od
  49. GROUP BY productCode
  50. ORDER BY low_stock DESC
  51. LIMIT 10;
  52. -- Product performance
  53. SELECT productCode,
  54. SUM(quantityOrdered * priceEach) AS prod_perf
  55. FROM orderdetails od
  56. GROUP BY productCode
  57. ORDER BY prod_perf DESC
  58. LIMIT 10;
  59. -- Priority Products for restocking
  60. WITH
  61. low_stock_table AS (
  62. SELECT productCode,
  63. ROUND(SUM(quantityOrdered) * 1.0/(SELECT quantityInStock
  64. FROM products p
  65. WHERE od.productCode = p.productCode), 2) AS low_stock
  66. FROM orderdetails od
  67. GROUP BY productCode
  68. ORDER BY low_stock DESC
  69. LIMIT 10
  70. )
  71. SELECT productCode,
  72. SUM(quantityOrdered * priceEach) AS prod_perf
  73. FROM orderdetails od
  74. WHERE productCode IN (SELECT productCode
  75. FROM low_stock_table)
  76. GROUP BY productCode
  77. ORDER BY prod_perf DESC
  78. LIMIT 10;
  79. /* Screen 5 */
  80. -- revenue by customer
  81. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  82. FROM products p
  83. JOIN orderdetails od
  84. ON p.productCode = od.productCode
  85. JOIN orders o
  86. ON o.orderNumber = od.orderNumber
  87. GROUP BY o.customerNumber;
  88. -- Top 5 VIP customers
  89. WITH
  90. money_in_by_customer_table AS (
  91. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  92. FROM products p
  93. JOIN orderdetails od
  94. ON p.productCode = od.productCode
  95. JOIN orders o
  96. ON o.orderNumber = od.orderNumber
  97. GROUP BY o.customerNumber
  98. )
  99. SELECT contactLastName, contactFirstName, city, country, mc.revenue
  100. FROM customers c
  101. JOIN money_in_by_customer_table mc
  102. ON mc.customerNumber = c.customerNumber
  103. ORDER BY mc.revenue DESC
  104. LIMIT 5;
  105. -- Top 5 less engaging customers
  106. WITH
  107. money_in_by_customer_table AS (
  108. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  109. FROM products p
  110. JOIN orderdetails od
  111. ON p.productCode = od.productCode
  112. JOIN orders o
  113. ON o.orderNumber = od.orderNumber
  114. GROUP BY o.customerNumber
  115. )
  116. SELECT contactLastName, contactFirstName, city, country, mc.revenue
  117. FROM customers c
  118. JOIN money_in_by_customer_table mc
  119. ON mc.customerNumber = c.customerNumber
  120. ORDER BY mc.revenue
  121. LIMIT 5;
  122. -- Customer LTV
  123. WITH
  124. money_in_by_customer_table AS (
  125. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  126. FROM products p
  127. JOIN orderdetails od
  128. ON p.productCode = od.productCode
  129. JOIN orders o
  130. ON o.orderNumber = od.orderNumber
  131. GROUP BY o.customerNumber
  132. )
  133. SELECT AVG(mc.revenue) AS ltv
  134. FROM money_in_by_customer_table mc;