-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchocolate_queries.sql
More file actions
180 lines (152 loc) · 5.02 KB
/
Copy pathchocolate_queries.sql
File metadata and controls
180 lines (152 loc) · 5.02 KB
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
175
USE Chocolate_DB;
-- Country query
-- Boxes shipped (Discarded the returns) - 1
SELECT
country,
COUNT(order_id) AS total_orders,
SUM(boxes_shipped) AS total_boxes,
SUM(amount_after_discount) AS total_revenue,
SUM(marketing_spend) AS total_marketing_spend,
SUM(amount_after_discount) / SUM(marketing_spend) AS marketing_roi,
AVG(amount_after_discount) AS avg_order_value
FROM invoices
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0 -- this is to discard the returns
GROUP BY country
ORDER BY total_revenue DESC;
-- Boxes returned - 2
SELECT
country,
COUNT(order_id) AS total_orders,
SUM(boxes_shipped) AS total_boxes,
SUM(amount_after_discount) AS total_revenue,
SUM(marketing_spend) AS total_marketing_spend,
SUM(amount_after_discount) / SUM(marketing_spend) AS marketing_roi,
AVG(amount_after_discount) AS avg_order_value
FROM invoices
WHERE amount_after_discount < 0 AND boxes_shipped < 0 -- this is to discard the returns
GROUP BY country
ORDER BY total_revenue DESC;
-- product by total boxes sold - 3
SELECT
p.product,
SUM(i.boxes_shipped) AS total_boxes,
SUM(i.amount_after_discount) AS total_revenue,
AVG(i.discount_pct) AS avg_discount
FROM invoices i
JOIN products p
ON i.product_id = p.product_id
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0 -- this is to discard the returns
GROUP BY p.product
ORDER BY total_boxes DESC;
-- product by revenue - 4
SELECT
p.product,
SUM(i.boxes_shipped) AS total_boxes,
SUM(i.amount_after_discount) AS total_revenue,
AVG(i.discount_pct) AS avg_discount
FROM invoices i
JOIN products p
ON i.product_id = p.product_id
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0 -- this is to discard the returns
GROUP BY p.product
ORDER BY total_revenue DESC;
-- channel performance - 5
SELECT
channel,
COUNT(order_id) AS total_orders,
SUM(boxes_shipped) AS total_boxes,
SUM(amount_after_discount) AS total_revenue,
AVG(discount_pct) AS avg_discount,
AVG(amount_after_discount) AS avg_order_value
FROM invoices
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0 -- this is to discard the returns
GROUP BY channel HAVING total_orders > 0 -- HERE YOU HAVE THE HAVING
ORDER BY total_revenue DESC;
-- Retail will be my own store or supermarket eg -> more extensive in logistics
-- wholesale is b2b or company that will do transformation
-- adjust prices or discounts to be less competitive in Wholesale - competitors may be much higher in price
-- MONTHLY - 6
SELECT
MONTH(order_date) AS sales_month,
SUM(amount_after_discount) AS monthly_revenue,
SUM(boxes_shipped) AS monthly_boxes
FROM invoices
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0 -- this is to discard the returns
GROUP BY MONTH(order_date)
ORDER BY sales_month;
-- need visualization to be done in python
-- TOP SALESPEOPLE - 7
SELECT
s.salesperson,
COUNT(i.order_id) AS total_orders,
SUM(i.amount_after_discount) AS total_revenue,
SUM(i.boxes_shipped) AS total_boxes,
AVG(i.discount_pct) AS avg_discount,
AVG(i.amount_after_discount) AS avg_order_value
FROM invoices i
JOIN salespersons s
ON i.salesperson_id = s.salesperson_id
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0 -- this is to discard the returns
GROUP BY s.salesperson
ORDER BY total_revenue DESC
LIMIT 10;
-- best product by country - 8 (Used to confirm products to be sold in the country we're going to open)
SELECT
country,
product,
total_revenue
FROM (
SELECT
i.country,
p.product,
SUM(i.amount_after_discount) AS total_revenue,
RANK() OVER (
PARTITION BY i.country
ORDER BY SUM(i.amount_after_discount) DESC
) AS product_rank
FROM invoices i
JOIN products p
ON i.product_id = p.product_id
WHERE i.amount_after_discount >= 0
AND i.boxes_shipped >= 0
GROUP BY i.country, p.product
) ranked_products
WHERE product_rank = 1;
-- on total boxes - 9
SELECT
country,
product,
total_boxes
FROM (
SELECT
i.country,
p.product,
SUM(i.boxes_shipped) AS total_boxes,
RANK() OVER (
PARTITION BY i.country
ORDER BY SUM(i.boxes_shipped) DESC
) AS product_rank
FROM invoices i
JOIN products p
ON i.product_id = p.product_id
WHERE i.amount_after_discount >= 0
AND i.boxes_shipped >= 0
GROUP BY i.country, p.product
) ranked_products
WHERE product_rank = 1;
-- DO DISCOUNT INCREASE VOLUME? - 10
SELECT
CASE
WHEN discount_pct = 0 THEN 'No discount'
WHEN discount_pct <= 10 THEN 'Low discount'
WHEN discount_pct <= 20 THEN 'Medium discount'
ELSE 'High discount'
END AS discount_group,
COUNT(order_id) AS total_orders,
AVG(boxes_shipped) AS avg_boxes_shipped,
SUM(amount_after_discount) AS total_revenue
FROM invoices
WHERE amount_after_discount >= 0 AND boxes_shipped >= 0
GROUP BY discount_group
ORDER BY total_revenue DESC;
-- We'll revise the high discount policy to increase sales