-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCase Study 1 - Danny's Diner
243 lines (198 loc) · 7.27 KB
/
Case Study 1 - Danny's Diner
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
CREATE SCHEMA dannys_diner;
SET search_path = dannys_diner;
--Creating the tables from Danny's Diner from #8weeksql challenge
--Table1.Sales
CREATE TABLE sales (
"customer_id" VARCHAR(1),
"order_date" DATE,
"product_id" INTEGER
);
INSERT INTO sales
("customer_id", "order_date", "product_id")
VALUES
('A', '2021-01-01', '1'),
('A', '2021-01-01', '2'),
('A', '2021-01-07', '2'),
('A', '2021-01-10', '3'),
('A', '2021-01-11', '3'),
('A', '2021-01-11', '3'),
('B', '2021-01-01', '2'),
('B', '2021-01-02', '2'),
('B', '2021-01-04', '1'),
('B', '2021-01-11', '1'),
('B', '2021-01-16', '3'),
('B', '2021-02-01', '3'),
('C', '2021-01-01', '3'),
('C', '2021-01-01', '3'),
('C', '2021-01-07', '3');
--Table2.Menu
CREATE TABLE menu (
"product_id" INTEGER,
"product_name" VARCHAR(5),
"price" INTEGER
);
INSERT INTO menu
("product_id", "product_name", "price")
VALUES
('1', 'sushi', '10'),
('2', 'curry', '15'),
('3', 'ramen', '12');
--Table3.Members
CREATE TABLE members (
"customer_id" VARCHAR(1),
"join_date" DATE
);
INSERT INTO members
("customer_id", "join_date")
VALUES
('A', '2021-01-07'),
('B', '2021-01-09');
--Visualise the table
SELECT * FROM sales;
SELECT * FROM menu;
SELECT * FROM members;
/* --------------------
Case Study Questions
--------------------*/
-- 1. What is the total amount each customer spent at the restaurant?
SELECT a.customer_id, SUM(b.price)
FROM sales AS a
INNER JOIN menu AS b
ON a.product_id = b.product_id
GROUP BY customer_id
ORDER BY customer_id;
-- 2. How many days has each customer visited the restaurant?
SELECT a.customer_id,
COUNT(DISTINCT(a.order_date)) AS No_of_days_visited
FROM sales AS a
GROUP BY a.customer_id;
-- 3. What was the first item from the menu purchased by each customer?
SELECT DISTINCT ON(a.customer_id) a.customer_id, a.order_date, b.product_id, b.product_name
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
ORDER BY a.customer_id, a.order_date, b.product_id;
-- 4. What is the most purchased item on the menu and how many times was it purchased by all customers?
SELECT a.product_id, b.product_name, COUNT(a.product_id) FROM sales AS a LEFT JOIN menu AS b
ON a.product_id = b.product_id GROUP BY a.product_id, b.product_name ORDER BY COUNT(a.product_id) DESC LIMIT 1;
-- 5. Which item was the most popular for each customer?
SELECT DISTINCT ON(a.customer_id) a.customer_id, b.product_name, COUNT(a.product_id)
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
GROUP BY a.customer_id, b.product_name
ORDER BY a.customer_id, COUNT(a.product_id) DESC;
SELECT a.customer_id, b.product_name, COUNT(a.product_id) AS total
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
GROUP BY a.customer_id, b.product_name
ORDER BY a.customer_id, COUNT(a.product_id) DESC;
--Using Dense Rank method
SELECT customer_id, product_name FROM
(SELECT a.customer_id, b.product_name,
DENSE_RANK() OVER
(PARTITION BY customer_id ORDER BY COUNT(a.product_id) DESC) product_rank
FROM sales AS a LEFT JOIN menu AS b
ON a.product_id = b.product_id GROUP BY a.customer_id, b.product_name)
AS ranking
WHERE product_rank = 1;
-- 6. Which item was purchased first by the customer after they became a member?
SELECT customer_id, product_name FROM
(SELECT a.customer_id, b.product_name, a.order_date,
DENSE_RANK() OVER
(PARTITION BY a.customer_id ORDER BY a.order_date) AS product_rank
FROM sales AS a LEFT JOIN menu AS b
ON a.product_id = b.product_id
LEFT JOIN members As c
ON a.customer_id = c.customer_id
WHERE a.order_date >= c.join_date)
AS ranking
WHERE product_rank = 1;
-- 7. Which item was purchased just before the customer became a member?
SELECT customer_id, product_name FROM
(SELECT a.customer_id, b.product_name, a.order_date,
DENSE_RANK() OVER
(PARTITION BY a.customer_id ORDER BY a.order_date DESC) AS product_rank
FROM sales AS a LEFT JOIN menu AS b
ON a.product_id = b.product_id
LEFT JOIN members As c
ON a.customer_id = c.customer_id
WHERE a.order_date < c.join_date)
AS ranking
WHERE product_rank = 1;
-- 8. What is the total items and amount spent for each member before they became a member?
--Using dense_rank method
SELECT customer_id, COUNT(product_id) AS total_items, SUM(price) AS total_amount FROM
(SELECT a.customer_id, b.product_name, a.order_date,a.product_id, b.price,
DENSE_RANK() OVER
(PARTITION BY a.customer_id ORDER BY a.order_date DESC) AS product_rank
FROM sales AS a LEFT JOIN menu AS b
ON a.product_id = b.product_id
LEFT JOIN members As c
ON a.customer_id = c.customer_id
WHERE a.order_date < c.join_date)
AS ranking
GROUP BY customer_id ORDER BY customer_id;
--Using simple join
SELECT a.customer_id, COUNT(a.product_id) AS total_items, SUM(b.price) AS total_amount
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
LEFT JOIN members As c
ON a.customer_id = c.customer_id
WHERE a.order_date < c.join_date
GROUP BY a.customer_id ORDER BY a.customer_id;
-- 9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?
SELECT a.customer_id,
SUM(CASE WHEN b.product_name = 'sushi' THEN 2*10*b.price
WHEN b.product_name != 'sushi' THEN 1*10*b.price
END) points
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id GROUP BY a.customer_id ORDER BY a.customer_id;
-- 10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A and B have at the end of January?
SELECT c.customer_id,
SUM(CASE WHEN c.join_date <= a.order_date and a.order_date < 7+ (c.join_date) THEN 2*10*b.price
ELSE 1*10*b.price
END) points
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
RIGHT JOIN members AS c
ON a.customer_id = c.customer_id
WHERE a.order_date < '2021-02-01' GROUP BY c.customer_id ORDER BY c.customer_id;
--Additional Challenges
-- Challenge-1: Creating basic data tables that Danny and his team can use to quickly derive insights without
-- needing to join the underlying tables using SQL.
SELECT a.customer_id, a.order_date, b.product_name, b.price,
(CASE WHEN c.join_date <= a.order_date THEN 'Y'
WHEN c.join_date > a.order_date THEN 'N'
ELSE 'N'
END) AS member
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
LEFT JOIN members AS c
ON a.customer_id = c.customer_id
ORDER BY a.customer_id, a.order_date, b.product_name;
-- Challenge-2: Danny also requires further information about the ranking of customer products, but he purposely does not
-- need the ranking for non-member purchases so he expects null ranking values for the records when customers are not
-- yet part of the loyalty program.
WITH initial_table AS (SELECT a.customer_id, a.order_date, b.product_name, b.price,
(CASE WHEN c.join_date <= a.order_date THEN 'Y'
ELSE 'N'
END) AS member
FROM sales AS a
LEFT JOIN menu AS b
ON a.product_id = b.product_id
LEFT JOIN members AS c
ON a.customer_id = c.customer_id
ORDER BY a.customer_id, a.order_date, b.product_name)
SELECT *,
(CASE WHEN member = 'Y' THEN
DENSE_RANK() OVER
(PARTITION BY customer_id, member ORDER BY order_date)
ELSE Null
END) AS ranking
FROM initial_table;