-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnswers.sql
312 lines (283 loc) · 6.91 KB
/
Answers.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
---- Question 1: What is the total amount each customer spent at the restaurant?
select
s.customer_id
, sum(m.price) as total_amount
from
dannys_diner.sales as s
join dannys_diner.menu as m
on s.product_id = m.product_id
group by
s.customer_id
---- Question 2: How many days has each customer visited the restaurant?
select
customer_id
, count(distinct order_date) as num_of_days_visited
from
dannys_diner.sales
group by
customer_id
---- Question 3: What was the first item from the menu purchased by each customer?
with X as
(select
customer_id
, min(order_date) as first_date
from
dannys_diner.sales
group by
customer_id
)
select
X.customer_id
, m.product_name
from X
join dannys_diner.sales as s
on s.customer_id = X.customer_id
and s.order_date = X.first_date
join dannys_diner.menu as m
on m.product_id = s.product_id
---- Question 4: What is the most purchased item on the menu and how many times was it purchased by all customers?
with X as
(select
m.product_name
, count(s.product_id) as counts
from
dannys_diner.sales as s
join dannys_diner.menu as m
on s.product_id = m.product_id
group by
m.product_name)
select
*
from X
order by
counts desc
limit 1
---- Question 5: Which item was the most popular for each customer?
with X as
(select
s.customer_id
, count(m.product_name) as num_of_order
, m.product_name
from
dannys_diner.sales as s
join dannys_diner.menu as m
on s.product_id = m.product_id
group by
s.customer_id
, m.product_name),
Y as
(select
X.*
, rank () over (partition by customer_id order by num_of_order desc) as ranks
from X)
select
customer_id
, product_name
, num_of_order
from Y
where
ranks = 1
---- Question 6: Which item was purchased first by the customer after they became a member?
with X as
(select
s.customer_id
, min(s.order_date) as member_day
from
dannys_diner.sales as s
join dannys_diner.members as mem
on s.customer_id = mem.customer_id
and ( s.order_date - mem.join_date) >= 0
group by
s.customer_id)
select
X.*
, m.product_name
from X
join dannys_diner.sales as s
on X.customer_id = s.customer_id
and X.member_day = s.order_date
join dannys_diner.menu as m
on s.product_id = m.product_id
---- Question 7: Which item was purchased just before the customer became a member?
with X as
(select
s.customer_id
, max(s.order_date) as member_day
from
dannys_diner.sales as s
join dannys_diner.members as mem
on s.customer_id = mem.customer_id
and ( s.order_date - mem.join_date) < 0
group by
s.customer_id
union all
select
s.customer_id
, max(s.order_date)
from
dannys_diner.sales as s
left join dannys_diner.members as mem
on s.customer_id = mem.customer_id
where
mem.customer_id is null
group by
s.customer_id)
select
X.*
, m.product_name
from X
join dannys_diner.sales as s
on X.customer_id = s.customer_id
and X.member_day = s.order_date
join dannys_diner.menu as m
on s.product_id = m.product_id
---- Question 8: What is the total items and amount spent for each member before they became a member?
with X as
(select
s.customer_id
, s.product_id
from
dannys_diner.sales as s
join dannys_diner.members as mem
on s.customer_id = mem.customer_id
and ( s.order_date - mem.join_date) < 0
union all
select
s.customer_id
, s.product_id
from
dannys_diner.sales as s
left join dannys_diner.members as mem
on s.customer_id = mem.customer_id
where
mem.customer_id is null)
select
X.customer_id
, count(X.product_id) as total_items
, sum(m.price) as total_amount
from X
join dannys_diner.menu as m
on m.product_id = X.product_id
group by
X.customer_id
| customer_id | total_items | total_amount |
| ----------- | ----------- | ------------ |
| A | 2 | 25 |
| B | 3 | 40 |
| C | 3 | 36 |
---- Question 9: If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?
with X as
(select
s.customer_id
, case
when m.product_name = 'sushi' then 20*m.price
else 10*m.price
end as point
from
dannys_diner.sales as s
join dannys_diner.menu as m
on s.product_id = m.product_id)
select
customer_id
, sum(point) as total_point
from X
group by
customer_id
---- Question 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?
with X as
(select
s.customer_id
, case
when m.product_name = 'sushi' then 20*m.price
else 10*m.price
end as point
from
dannys_diner.sales as s
join dannys_diner.members as mem
on s.customer_id = mem.customer_id
and ( s.order_date - mem.join_date) < 0
join dannys_diner.menu as m
on m.product_id = s.product_id
where
(s.order_date - '2021-01-31') <= 0
union all
select
s.customer_id
, 20*m.price as point
from
dannys_diner.sales as s
join dannys_diner.members as mem
on s.customer_id = mem.customer_id
and ( s.order_date - mem.join_date) > 0
join dannys_diner.menu as m
on m.product_id = s.product_id
where
(s.order_date - '2021-01-31') > 0
)
select
customer_id
, sum(point) as total_point
from X
group by
customer_id
---- Bonus question:
select
s.customer_id
, s.order_date
, m.product_name
, m.price
, case
when mem.join_date is null then 'N'
when s.order_date - mem.join_date < 0 then 'N'
else 'Y'
end as member
from
dannys_diner.sales as s
left join dannys_diner.members as mem
on s.customer_id = mem.customer_id
join dannys_diner.menu as m
on m.product_id = s.product_id
order by
customer_id
, order_date
---- Bonus question:
with X as
(select
s.customer_id
, s.order_date
, m.product_name
, m.price
, 'Y' as member
, rank() over (partition by s.customer_id order by s.order_date asc) as ranking
from
dannys_diner.sales as s
join dannys_diner.members as mem
on s.customer_id = mem.customer_id
and s.order_date - mem.join_date >= 0
join dannys_diner.menu as m
on m.product_id = s.product_id
union all
select
s.customer_id
, s.order_date
, m.product_name
, m.price
, 'N' as member
, null as ranking
from
dannys_diner.sales as s
left join dannys_diner.members as mem
on s.customer_id = mem.customer_id
join dannys_diner.menu as m
on m.product_id = s.product_id
where
mem.join_date is null
or s.order_date - mem.join_date < 0
)
select
*
from X
order by
customer_id
, order_date