123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /* Screen 3 */
- -- Table descriptions
- SELECT 'Customers' AS table_name,
- 13 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM Customers
-
- UNION ALL
- SELECT 'Products' AS table_name,
- 9 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM Products
- UNION ALL
- SELECT 'ProductLines' AS table_name,
- 4 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM ProductLines
- UNION ALL
- SELECT 'Orders' AS table_name,
- 7 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM Orders
- UNION ALL
- SELECT 'OrderDetails' AS table_name,
- 5 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM OrderDetails
- UNION ALL
- SELECT 'Payments' AS table_name,
- 4 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM Payments
- UNION ALL
- SELECT 'Employees' AS table_name,
- 8 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM Employees
- UNION ALL
- SELECT 'Offices' AS table_name,
- 9 AS number_of_attribute,
- COUNT(*) AS number_of_row
- FROM Offices;
- /* Screen 4 */
- --Low stock
- SELECT productCode,
- ROUND(SUM(quantityOrdered) * 1.0 / (SELECT quantityInStock
- FROM products p
- WHERE od.productCode = p.productCode), 2) AS low_stock
- FROM orderdetails od
- GROUP BY productCode
- ORDER BY low_stock
- LIMIT 10;
-
- -- Product performance
- SELECT productCode,
- SUM(quantityOrdered * priceEach) AS prod_perf
- FROM orderdetails od
- GROUP BY productCode
- ORDER BY prod_perf DESC
- LIMIT 10;
-
- -- Priority Products for restocking
- WITH
- low_stock_table AS (
- SELECT productCode,
- ROUND(SUM(quantityOrdered) * 1.0/(SELECT quantityInStock
- FROM products p
- WHERE od.productCode = p.productCode), 2) AS low_stock
- FROM orderdetails od
- GROUP BY productCode
- ORDER BY low_stock
- LIMIT 10
- )
- SELECT productCode,
- SUM(quantityOrdered * priceEach) AS prod_perf
- FROM orderdetails od
- WHERE productCode IN (SELECT productCode
- FROM low_stock_table)
- GROUP BY productCode
- ORDER BY prod_perf DESC
- LIMIT 10;
- /* Screen 5 */
- -- revenue by customer
- SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
- FROM products p
- JOIN orderdetails od
- ON p.productCode = od.productCode
- JOIN orders o
- ON o.orderNumber = od.orderNumber
- GROUP BY o.customerNumber;
-
- -- Top 5 VIP customers
- WITH
- money_in_by_customer_table AS (
- SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
- FROM products p
- JOIN orderdetails od
- ON p.productCode = od.productCode
- JOIN orders o
- ON o.orderNumber = od.orderNumber
- GROUP BY o.customerNumber
- )
- SELECT contactLastName, contactFirstName, city, country, mc.revenue
- FROM customers c
- JOIN money_in_by_customer_table mc
- ON mc.customerNumber = c.customerNumber
- ORDER BY mc.revenue DESC
- LIMIT 5;
-
- -- Top 5 less engaging customers
- WITH
- money_in_by_customer_table AS (
- SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
- FROM products p
- JOIN orderdetails od
- ON p.productCode = od.productCode
- JOIN orders o
- ON o.orderNumber = od.orderNumber
- GROUP BY o.customerNumber
- )
- SELECT contactLastName, contactFirstName, city, country, mc.revenue
- FROM customers c
- JOIN money_in_by_customer_table mc
- ON mc.customerNumber = c.customerNumber
- ORDER BY mc.revenue
- LIMIT 5;
-
- -- Customer LTV
- WITH
- money_in_by_customer_table AS (
- SELECT o.customerNumber, SUM(quantityOrdered * (priceEach - buyPrice)) AS revenue
- FROM products p
- JOIN orderdetails od
- ON p.productCode = od.productCode
- JOIN orders o
- ON o.orderNumber = od.orderNumber
- GROUP BY o.customerNumber
- )
- SELECT AVG(mc.revenue) AS ltv
- FROM money_in_by_customer_table mc;
-
|