-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCase_Study_2.sql
More file actions
341 lines (296 loc) · 9.97 KB
/
Case_Study_2.sql
File metadata and controls
341 lines (296 loc) · 9.97 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
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
CREATE SCHEMA case_study_2;
SET search_path = case_study_2;
SHOW search_path;
CREATE TABLE IF NOT EXISTS case_study_2.pizza_names
(
pizza_id INTEGER ,
pizza_name TEXT,
CONSTRAINT pizza_names_pkey PRIMARY KEY (pizza_id)
);
INSERT INTO case_study_2.pizza_names
VALUES (1, 'Meatlovers'),
(2, 'Vegetarian');
CREATE TABLE IF NOT EXISTS case_study_2.pizza_toppings
(
topping_id INTEGER,
topping_name TEXT,
CONSTRAINT pizza_toppings_pkey PRIMARY KEY (topping_id)
);
INSERT INTO case_study_2.pizza_toppings
VALUES (1, 'Chesse'),
(2, 'Mushrooms'),
(3, 'Tomatoes'),
(4, 'Peperoni'),
(5, 'Chicken');
CREATE TABLE IF NOT EXISTS case_study_2.pizza_recipes
(
pizza_id INTEGER,
toppings TEXT,
CONSTRAINT pizza_recipes_pkey PRIMARY KEY (pizza_id)
);
INSERT INTO case_study_2.pizza_recipes
VALUES (1, '1,2,4'),
(2, '2,5'),
(3, '1,4,5'),
(4, '1,3'),
(5, '2,3');
CREATE TABLE IF NOT EXISTS case_study_2.runners
(
runner_id INTEGER,
registration_date DATE,
CONSTRAINT runners_pkey PRIMARY KEY (runner_id)
);
INSERT INTO case_study_2.runners
VALUES (1, '2021-01-01 18:20:19'),
(2, '2021-01-02 07:00:00'),
(3, '2021-01-02 11:25:59'),
(4, '2021-01-03 14:03:00'),
(5, '2021-01-04 08:11:11'),
(6, '2021-01-06 20:09:55'),
(7, '2021-01-06 21:30:30');
CREATE TABLE IF NOT EXISTS case_study_2.runner_orders
(
order_id INTEGER,
runner_id INTEGER,
pickup_time VARCHAR(19),
distance VARCHAR(7),
duration VARCHAR(10),
cancellation VARCHAR(23),
CONSTRAINT order_pkey PRIMARY KEY (order_id),
CONSTRAINT runner_fkey FOREIGN KEY (runner_id) REFERENCES case_study_2.runners(runner_id)
);
INSERT INTO case_study_2.runner_orders
VALUES (1, 1, '2021-01-01 18:15:34', '20km', '32 minutes', ''),
(2, 1, '2021-01-01 19:10:54', '20km', '27 minutes', ''),
(3, 1, '2021-01-03 00:12:37', '13.4km', '20 minutes', null),
(4, 2, '2021-01-04 13:53:03', '23.4', '40', null),
(5, 3, '2021-01-08 21:10:57', '10', '15', null),
(6, 3, null, null, null, 'Restaurant Cancellation'),
(7, 2, '2021-01-08 21:30:45', '25km', '25mins', null),
(8, 2, '2021-01-10 00:15:02', '23.4 km', '15 minute', null),
(9, 2, null, null, null, 'Customer Cancellation'),
(10, 1, '2021-01-11 18:50:20', '10km', '10minutes', null);
CREATE TABLE IF NOT EXISTS case_study_2.customer_orders
(
order_id INTEGER,
customer_id INTEGER,
pizza_id INTEGER,
exclusions VARCHAR(4),
extras VARCHAR(4),
order_date TIMESTAMP,
CONSTRAINT order_fk FOREIGN KEY (order_id) REFERENCES case_study_2.runner_orders(order_id),
CONSTRAINT pizza_name_fk FOREIGN KEY(pizza_id) REFERENCES case_study_2.pizza_names(pizza_id),
CONSTRAINT pizza_recpes_fk FOREIGN KEY(pizza_id) REFERENCES case_study_2.pizza_recipes(pizza_id)
);
INSERT INTO case_study_2.customer_orders
VALUES (1, 101, 1, '', '', '2021-01-01 18:05:02.000'),
(2, 101, 1, '', '', '2021-01-01 19:00:52.000'),
(3, 102, 1, '', '', '2021-01-02 23:51:23.000'),
(3, 102, 2, '', null, '2021-01-02 23:51:23.000'),
(4, 103, 1, 4, '', '2021-01-04 13:23:46.000'),
(4, 103, 1, 4, '', '2021-01-04 13:23:46.000'),
(4, 103, 2, 4, '', '2021-01-04 13:23:46.000'),
(5, 104, 1, null, 1, '2021-01-08 21:00:29.000'),
(6, 101, 2, null, null, '2021-01-08 21:03:13.000'),
(7, 105, 2, null, 1, '2021-01-08 21:20:29.000'),
(8, 102, 1, null, null, '2021-01-09 23:54:33.000'),
(9, 103, 1, 4, '1,5', '2021-01-10 11:22:59.000'),
(10, 104, 1, null, null, '2021-01-11 18:34:49.000'),
(10, 104, 1, '2,6', '1,4', '2021-01-09 23:54:33.000');
-- SELECT
SELECT * FROM case_study_2.pizza_names;
SELECT * FROM case_study_2.pizza_recipes;
SELECT * FROM case_study_2.pizza_toppings;
SELECT * FROM case_study_2.runners;
SELECT * FROM case_study_2.runner_orders;
SELECT * FROM case_study_2.customer_orders;
-- Create temporary table with all columns don't have values is null and if have values is null then replace with blank space ''
CREATE TEMP TABLE customer_orders_temp AS
SELECT
order_id,
customer_id,
pizza_id,
CASE
WHEN exclusions IS NULL OR exclusions LIKE 'null' THEN ''
ELSE exclusions
END AS exclusions,
CASE
WHEN extras IS NULL OR extras LIKE 'null' THEN ''
ELSE extras
END AS extras,
order_date
FROM case_study_2.customer_orders;
SELECT * FROM case_study_2.customer_orders;
SELECT * FROM customer_orders_temp;
-- In pickup_time column, remove nulls and replace with blank space ''
-- In distance column, remove "km" and nulls and replace with blank space ''
-- In duration column, remove "minute" and nulls and replace with blank space ''
-- In cancellation column, remove null and replace with blank psace ''
--
SELECT
order_id,
runner_id,
CASE
WHEN pickup_time IS NULL OR pickup_time LIKE 'null' THEN ''
ELSE pickup_time
END AS pickup_time,
CASE
WHEN distance IS NULL OR distance LIKE 'null' THEN ''
WHEN distance LIKE '%km' THEN TRIM('km' FROM distance)
ELSE distance
END AS distance,
CASE
WHEN duration IS NULL OR duration LIKE 'null' THEN ''
WHEN duration LIKE '%mins' THEN TRIM('mins' FROM duration)
WHEN duration LIKE '%minute' THEN TRIM ('minute' FROM duration)
WHEN duration LIKE '%minutes' THEN TRIM ('minutes' FROM duration)
ELSE duration
END AS duration,
CASE
WHEN cancellation IS NULL OR LIKE 'null' THEN ''
ELSE cancellation
END AS cancellation
FROM case_study_2.runner_orders;
SELECT
runner_id,
COUNT(order_id) AS successful_orders
FROM runner_orders
WHERE distance != 0
GROUP BY runner_id;
-- A. Pizza Metrics
-- 1. How many pizzas were ordered ?
SELECT COUNT(1) AS count_ordered
FROM customer_orders_temp
-- 2. How many unique customer orders were made ?
SELECT COUNT(DISTINCT order_id) FROM
customer_orders_temp
-- 3. How many successful orders were delivered by each runner ?
SELECT runner_id,
COUNT(1) AS count_order_success
FROM case_study_2.runner_orders
WHERE distance IS NOT NULL
AND TRIM('km' FROM distance)::FLOAT > 0
GROUP BY runner_id
ORDER BY runner_id
-- 4. How many of each type of pizza was delivered ?
WITH pizza_delivered AS (
SELECT co.pizza_id,
COUNT(ro.order_id) AS count_order_delivered
FROM case_study_2.runner_orders AS ro
JOIN case_study_2.customer_orders AS co
ON ro.order_id = co.order_id
WHERE ro.distance IS NOT NULL
AND TRIM('km' FROM ro.distance)::FLOAT > 0
GROUP BY co.pizza_id
)
SELECT pn.pizza_name, pd.count_order_delivered FROM pizza_delivered AS pd
JOIN case_study_2.pizza_names AS pn
ON pd.pizza_id = pn.pizza_id
-- 5. How many Vegetarian and Meatlovers were ordered by each customer ?
SELECT co.customer_id, pn.pizza_name,
COUNT(pn.pizza_name)
FROM case_study_2.customer_orders AS co
JOIN case_study_2.pizza_names AS pn ON co.pizza_id = pn.pizza_id
GROUP BY co.customer_id, pn.pizza_name
ORDER BY co.customer_id
-- 6. What was the maximum number of pizzas delivered in a single order ?
WITH pizza_count_cte AS (
SELECT c.order_id, COUNT(1) as count_order
FROM case_study_2.customer_orders AS c
JOIN case_study_2.runner_orders AS r
ON c.order_id = r.order_id
WHERE r.distance IS NOT NULL
AND TRIM('km' FROM r.distance)::FLOAT > 0
GROUP BY c.order_id
)
SELECT *
FROM pizza_count_cte
ORDER BY count_order DESC
LIMIT 1
-- 7. For each customer, how many delivered pizzas had at least change and how many had no changes ?
SELECT c.customer_id,
SUM(
CASE WHEN c.exclusions <> '' OR c.extras <> '' THEN 1
ELSE 0
END
) AS least_1_change,
SUM(
CASE WHEN (c.exclusions = '' OR c.exclusions IS NULL)
AND
(c.extras = '' OR c.extras IS NULL )
THEN 1
ELSE 0
END
) AS no_change
FROM case_study_2.customer_orders AS c
JOIN case_study_2.runner_orders AS r
ON c.order_id = r.order_id
WHERE r.distance IS NOT NULL
AND TRIM('km' FROM r.distance)::FLOAT > 0
GROUP BY c.customer_id
ORDER BY c.customer_id
-- 8. How many pizzas were delivered that had both exclusions and extras
SELECT SUM(
CASE
WHEN c.exclusions IS NOT NULL AND c.extras IS NOT NULL THEN 1
ELSE 0
END
) AS pizza_count_w_exclusions_extras
FROM case_study_2.customer_orders AS c
JOIN case_study_2.runner_orders as r
ON c.order_id = r.order_id
WHERE r.distance IS NOT NULL AND TRIM('km' FROM r.distance)::FLOAT > 0
AND c.exclusions <> ''
AND c.extras <> ''
-- 9. What is the total volume of pizza ordered for each hour of the day ?
SELECT DATE_PART('hour', c.order_date) AS hour_of_day,
COUNT(c.order_id) AS pizza_count
FROM case_study_2.customer_orders c
GROUP BY DATE_PART('hour', c.order_date)
-- 10. What is the volume of orders for each of the week
SELECT TO_CHAR(c.order_date, 'DAY') AS day_of_week,
COUNT(c.order_id) AS count_order
FROM case_study_2.customer_orders c
GROUP BY TO_CHAR(c.order_date, 'DAY')
-- B. Runner and Customer Experience
-- 1. How many runners signed up for each 1 week period ?
SELECT DATE_PART('WEEK', registration_date) AS registration_week,
COUNT(runner_id) AS runner_signup
FROM case_study_2.runners
GROUP BY DATE_PART('WEEK', registration_date)
-- 2. What was the average time in minutes it took for each runner to arrive at the Pizza Runner HQ to pickup the order?
WITH time_taken_cte AS (
SELECT
c.order_id,
c.order_date,
r.pickup_time,
EXTRACT(EPOCH FROM (r.pickup_time::TIMESTAMP - c.order_date)) / 60 AS pickup_minutes
FROM case_study_2.customer_orders c
JOIN case_study_2.runner_orders r
ON c.order_id = r.order_id
WHERE r.distance IS NOT NULL AND TRIM('km' FROM r.distance)::FLOAT > 0
GROUP BY c.order_id, c.order_date, r.pickup_time
)
SELECT ROUND(AVG(pickup_minutes), 2)
FROM time_taken_cte
WHERE pickup_minutes > 1;
-- 3. Is there any relationship between the number of pizzas and how long the order takes to prepare?
WITH prep_time_cte AS (
SELECT
c.order_id,
COUNT(c.order_id) AS pizza_order,
c.order_date,
r.pickup_time,
EXTRACT(EPOCH FROM (r.pickup_time::TIMESTAMP - c.order_date)) / 60 AS prep_time_minutes
FROM case_study_2.customer_orders c
JOIN case_study_2.runner_orders r
ON c.order_id = r.order_id
WHERE r.distance IS NOT NULL AND TRIM('km' FROM r.distance)::FLOAT > 0
GROUP BY c.order_id, c.order_date, r.pickup_time
)
SELECT
pizza_order,
ROUND(AVG(prep_time_minutes), 3) AS avg_prep_time_minutes
FROM prep_time_cte
WHERE prep_time_minutes > 1
GROUP BY pizza_order;