Queer European MD passionate about IT

600Solutions.sql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. products_to_restock AS (
  72. SELECT productCode,
  73. SUM(quantityOrdered * priceEach) AS prod_perf
  74. FROM orderdetails od
  75. WHERE productCode IN (SELECT productCode
  76. FROM low_stock_table)
  77. GROUP BY productCode
  78. ORDER BY prod_perf DESC
  79. LIMIT 10
  80. )
  81. SELECT productName, productLine
  82. FROM products AS p
  83. WHERE productCode IN (SELECT productCode
  84. FROM products_to_restock);
  85. /* Screen 5 */
  86. -- revenue by customer
  87. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  88. FROM products p
  89. JOIN orderdetails od
  90. ON p.productCode = od.productCode
  91. JOIN orders o
  92. ON o.orderNumber = od.orderNumber
  93. GROUP BY o.customerNumber;
  94. -- Top 5 VIP customers
  95. WITH
  96. money_in_by_customer_table AS (
  97. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  98. FROM products p
  99. JOIN orderdetails od
  100. ON p.productCode = od.productCode
  101. JOIN orders o
  102. ON o.orderNumber = od.orderNumber
  103. GROUP BY o.customerNumber
  104. )
  105. SELECT contactLastName, contactFirstName, city, country, mc.revenue
  106. FROM customers c
  107. JOIN money_in_by_customer_table mc
  108. ON mc.customerNumber = c.customerNumber
  109. ORDER BY mc.revenue DESC
  110. LIMIT 5;
  111. -- Top 5 less engaging customers
  112. WITH
  113. money_in_by_customer_table AS (
  114. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  115. FROM products p
  116. JOIN orderdetails od
  117. ON p.productCode = od.productCode
  118. JOIN orders o
  119. ON o.orderNumber = od.orderNumber
  120. GROUP BY o.customerNumber
  121. )
  122. SELECT contactLastName, contactFirstName, city, country, mc.revenue
  123. FROM customers c
  124. JOIN money_in_by_customer_table mc
  125. ON mc.customerNumber = c.customerNumber
  126. ORDER BY mc.revenue
  127. LIMIT 5;
  128. -- Customer LTV
  129. WITH
  130. money_in_by_customer_table AS (
  131. SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
  132. FROM products p
  133. JOIN orderdetails od
  134. ON p.productCode = od.productCode
  135. JOIN orders o
  136. ON o.orderNumber = od.orderNumber
  137. GROUP BY o.customerNumber
  138. )
  139. SELECT AVG(mc.revenue) AS ltv
  140. FROM money_in_by_customer_table mc;