-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
600Solutions.sql
174 lines (140 loc) · 4.01 KB
/
600Solutions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* 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 DESC
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 DESC
LIMIT 10
),
products_to_restock AS (
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
)
SELECT productName, productLine
FROM products AS p
WHERE productCode IN (SELECT productCode
FROM products_to_restock);
/* 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;