-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zomato Query.sql
388 lines (354 loc) · 7.47 KB
/
Zomato Query.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
USE zomato;
/* Question 1: What is the total amount each customer spent? */
SELECT
s.userid,
sum(p.price) AS total_spent
FROM
sales s
LEFT JOIN product p
ON p.product_id = p.product_id
GROUP BY
s.userid
ORDER BY
total_spent DESC;
/* Question 2: How many days has each customer ordered on Zomato? */
SELECT
userid,
COUNT(DISTINCT created_date) AS days_count
FROM
sales
GROUP BY
userid
ORDER BY
days_count DESC;
/* Question 3: Which was the first product purchased by each customer? */
WITH order_rank AS(
SELECT
userid,
created_date,
product_id,
DENSE_RANK() OVER(PARTITION BY userid ORDER BY created_date) AS rank_day
FROM
sales)
SELECT
userid,
product_id
FROM
order_rank
WHERE
rank_day=1;
/* Question 4: Which is the most purchased item on the menu and how many times was this item purchased by all customers? */
WITH popularity AS(
SELECT
product_id,
COUNT(*) AS total_order_count
FROM
sales
GROUP BY
product_id)
SELECT
userid,
s.product_id,
count(*) AS order_count,
total_order_count
FROM
sales s
JOIN popularity p ON s.product_id = p.product_id
WHERE
total_order_count = (SELECT MAX(total_order_count) FROM popularity)
GROUP BY
userid, s.product_id
ORDER BY
s.product_id;
/* Question 5: Which item was the most popular for each customer? */
WITH order_count AS(
SELECT
userid,
product_id,
count(*) AS order_counts
FROM
sales
GROUP BY
userid, product_id),
orders_rank AS
(SELECT
userid,
product_id,
order_counts,
DENSE_RANK() OVER(PARTITION BY userid ORDER BY order_counts DESC) AS order_rank
FROM
order_count)
SELECT
userid,
product_id,
order_counts
FROM
orders_rank
WHERE
order_rank =1
ORDER BY
order_counts DESC;
/* Question 6: Which item was purchased immediately after joining membership by users? */
WITH order_rank AS(
SELECT
sales.userid,
gold_signup_date,
created_date,
product_id,
ROW_NUMBER() OVER(PARTITION BY sales.userid ORDER BY created_date) AS order_rank
FROM
sales
INNER JOIN goldusers_signup gold
ON sales.userid = gold.userid
WHERE
created_date >= gold_signup_date)
SELECT
userid,
created_date,
gold_signup_date,
product_id
FROM
order_rank
WHERE
order_rank =1
ORDER BY
userid;
/* Question 7: Which item was purchased immediately before joining membership by users? */
WITH order_rank AS(
SELECT
sales.userid,
created_date,
gold_signup_date,
product_id,
DENSE_RANK() OVER(PARTITION BY sales.userid ORDER BY created_date DESC) AS order_rank
FROM
sales
INNER JOIN goldusers_signup gold
ON sales.userid = gold.userid
WHERE
created_date < gold_signup_date)
SELECT
userid,
gold_signup_date,
created_date,
product_id
FROM
order_rank
WHERE
order_rank =1
ORDER BY
userid;
/* Question 8: What are the total orders placed and the amount spent by each gold member before they became a member? */
WITH order_summary AS(
SELECT
sales.userid,
COUNT(created_date) AS total_order,
SUM(price) AS total_amount_spent
FROM
sales
INNER JOIN goldusers_signup gold
ON sales.userid = gold.userid
INNER JOIN product ON sales.product_id = product.product_id
WHERE
created_date < gold_signup_date
GROUP BY
sales.userid)
SELECT
userid,
total_order,
total_amount_spent
FROM
order_summary
ORDER BY
total_amount_spent DESC;
/* Question 9: What are the total orders and the amount spent for each gold member after they became a member? */
WITH order_summary AS(
SELECT
sales.userid,
COUNT(created_date) AS total_order,
SUM(price) AS total_amount_spent
FROM
sales
INNER JOIN goldusers_signup gold
ON sales.userid = gold.userid
INNER JOIN product ON sales.product_id = product.product_id
WHERE
created_date >= gold_signup_date
GROUP BY
sales.userid)
SELECT
userid,
total_order,
total_amount_spent
FROM
order_summary
ORDER BY
total_amount_spent DESC;
/* Question 10:
Zomato Points:
2 Zomato points = 5 Rs. Cashback
Zomato Points Calculation:
Each 5 rupees spent: 2 Zomato points
AND
Product 1: Each 5 rupees spent: 1 Zomato point
Product 2: Each 10 rupees spent: 5 Zomato points
Product 3: Each 5 rupees spent: 1 Zomato point
What is the amount of total Zomato points collected by each user?
*/
WITH total_spents1 AS(
SELECT
s.userid,
sum(p.price) AS total_spent1
FROM
sales s
INNER JOIN product p
ON s.product_id = p.product_id
GROUP BY
s.userid),
total_spents2 AS(
SELECT
s.userid,
s.product_id,
sum(p.price) AS total_spent2
FROM
sales s
INNER JOIN product p
ON s.product_id = p.product_id
GROUP BY
s.userid, s.product_id),
zomato_points AS(
SELECT
total_spents1.userid,
ROUND(total_spent1/5)*2 AS total_spent_points,
SUM(CASE
WHEN total_spents2.product_id = 1 THEN ROUND(total_spent2/5)*1
WHEN total_spents2.product_id = 2 THEN ROUND(total_spent2/10)*5
WHEN total_spents2.product_id = 3 THEN ROUND(total_spent2/5)*1
ELSE 0
END) AS product_spent_points
FROM
total_spents1
INNER JOIN total_spents2
ON total_spents1.userid = total_spents2.userid
GROUP BY
total_spents1.userid)
SELECT
userid,
(total_spent_points+product_spent_points) AS zomato_points,
(total_spent_points+product_spent_points)*2.5 AS cashback_earned
FROM
zomato_points
ORDER BY
zomato_points DESC;
/* Question 11:
Zomato Points:
2 Zomato points = 5 Rs. Cashback
Zomato Points Calculation:
Each 5 rupees spent: 2 Zomato points
AND
Product 1: Each 5 rupees spent: 1 Zomato point
Product 2: Each 10 rupees spent: 5 Zomato points
Product 3: Each 5 rupees spent: 1 Zomato point
For which product have the most Zomato points been given?
*/
WITH total_spents AS(
SELECT
s.userid,
s.product_id,
sum(p.price) AS total_spent
FROM
sales s
INNER JOIN product p
ON s.product_id = p.product_id
GROUP BY
s.userid, s.product_id),
zomato_points AS(
SELECT
product_id,
SUM(CASE
WHEN product_id = 1 THEN ROUND(total_spent/5)*1
WHEN product_id = 2 THEN ROUND(total_spent/10)*5
WHEN product_id = 3 THEN ROUND(total_spent/5)*1
ELSE 0
END) AS product_spent_points
FROM
total_spents
GROUP BY
product_id)
SELECT
product_id,
product_spent_points
FROM
zomato_points
WHERE
product_spent_points = (SELECT MAX(product_spent_points) FROM zomato_points);
/* In the first year after a customer joins the gold program (including their joining date),
irrespective of what the customer has purchased, they earn 5 Zomato points for every 10 Rs.
spent. Which gold user earned the most in the first year after joining the gold program? */
WITH zomato_points AS(
SELECT
gold.userid,
ROUND(SUM(p.price)/10)*5 AS total_points
FROM
goldusers_signup gold
LEFT JOIN
sales
ON gold.userid = sales.userid
JOIN
product p
ON p.product_id = sales.product_id
WHERE
created_date >= gold_signup_date AND created_date <= DATE_ADD(gold_signup_date, INTERVAL 1 YEAR)
GROUP BY
gold.userid)
SELECT
userid,
total_points
FROM
zomato_points
WHERE
total_points = (SELECT MAX(total_points) FROM zomato_points);
/* Rank all the monthly transactions of each year by the customers. */
WITH years AS (
SELECT
EXTRACT(YEAR FROM created_date) AS years,
EXTRACT(MONTH FROM created_date) AS months,
created_date,
userid
FROM
sales)
SELECT
userid,
years,
months,
created_date,
DENSE_RANK() OVER (PARTITION BY years, months ORDER BY created_date) AS txn_rank
FROM
years
ORDER BY
years, months, txn_rank, userid;
/* Question 14: Rank all the monthly transactions of each year by the gold customers. Mark 'Non-Gold-Member' for non-gold customers. */
WITH years AS (
SELECT
EXTRACT(YEAR FROM created_date) AS years,
EXTRACT(MONTH FROM created_date) AS months,
created_date,
userid
FROM
sales)
SELECT
y.userid,
years,
months,
created_date,
CASE
WHEN y.userid = gold.userid AND created_date >= gold_signup_date THEN DENSE_RANK() OVER (PARTITION BY years, months ORDER BY created_date)
ELSE "Non-Gold-Member"
END AS txn_rank
FROM
years y
LEFT JOIN
goldusers_signup gold
ON gold.userid = y.userid
ORDER BY
years, months, txn_rank, userid;