-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathquery-sql.sql
686 lines (614 loc) · 31.1 KB
/
query-sql.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/* --------------------
Table Transformation
--------------------*/
-- data type check
--customer_orders
SELECT
table_name,
column_name,
data_type
FROM information_schema.columns
WHERE table_name = 'customer_orders';
--Result:
+──────────────────+──────────────+──────────────────────────────+
| table_name | column_name | data_type |
+──────────────────+──────────────+──────────────────────────────+
| customer_orders | order_id | integer |
| customer_orders | customer_id | integer |
| customer_orders | pizza_id | integer |
| customer_orders | exclusions | character varying |
| customer_orders | extras | character varying |
| customer_orders | order_time | timestamp without time zone |
+──────────────────+──────────────+──────────────────────────────+
| table_name | column_name | data_type |
|-----------------|-------------|-----------------------------|
| customer_orders | order_id | integer |
| customer_orders | customer_id | integer |
| customer_orders | pizza_id | integer |
| customer_orders | exclusions | character varying |
| customer_orders | extras | character varying |
| customer_orders | order_time | timestamp without time zone |
--runner_orders
SELECT
table_name,
column_name,
data_type
FROM information_schema.columns
WHERE table_name = 'runner_orders';
--Result:
+────────────────+──────────────+────────────────────+
| table_name | column_name | data_type |
+────────────────+──────────────+────────────────────+
| runner_orders | order_id | integer |
| runner_orders | runner_id | integer |
| runner_orders | pickup_time | character varying |
| runner_orders | distance | character varying |
| runner_orders | duration | character varying |
| runner_orders | cancellation | character varying |
+────────────────+──────────────+────────────────────+
| table_name | column_name | data_type |
|---------------|--------------|-------------------|
| runner_orders | order_id | integer |
| runner_orders | runner_id | integer |
| runner_orders | pickup_time | character varying |
| runner_orders | distance | character varying |
| runner_orders | duration | character varying |
| runner_orders | cancellation | character varying |
--Update tables
--1. customer_order
/*
Cleaning customer_orders
- Identify records with null or 'null' values
- updating null or 'null' values to ''
- blanks '' are not null because it indicates the customer asked for no extras or exclusions
*/
--Blanks indicate that the customer requested no extras/exclusions for the pizza, whereas null values would be ambiguous on this.
DROP TABLE IF EXISTS updated_customer_orders;
CREATE TEMP TABLE updated_customer_orders 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_time
FROM pizza_runner.customer_orders
);
SELECT * FROM updated_customer_orders;
--Result:
|order_id|customer_id|pizza_id|exclusions|extras|order_time |
|--------|-----------|--------|----------|------|------------------------|
|1 |101 |1 | | |2020-01-01T18:05:02.000Z|
|2 |101 |1 | | |2020-01-01T19:00:52.000Z|
|3 |102 |1 | | |2020-01-02T12:51:23.000Z|
|3 |102 |2 | | |2020-01-02T12:51:23.000Z|
|4 |103 |1 |4 | |2020-01-04T13:23:46.000Z|
|4 |103 |1 |4 | |2020-01-04T13:23:46.000Z|
|4 |103 |2 |4 | |2020-01-04T13:23:46.000Z|
|5 |104 |1 | |1 |2020-01-08T21:00:29.000Z|
|6 |101 |2 | | |2020-01-08T21:03:13.000Z|
|7 |105 |2 | |1 |2020-01-08T21:20:29.000Z|
|8 |102 |1 | | |2020-01-09T23:54:33.000Z|
|9 |103 |1 |4 |1, 5 |2020-01-10T11:22:59.000Z|
|10 |104 |1 | | |2020-01-11T18:34:49.000Z|
|10 |104 |1 |2, 6 |1, 4 |2020-01-11T18:34:49.000Z|
*/
+───────────+──────────────+───────────+─────────────+─────────+───────────────────────────+
| order_id | customer_id | pizza_id | exclusions | extras | order_time |
+───────────+──────────────+───────────+─────────────+─────────+───────────────────────────+
| 1 | 101 | 1 | | | 2020-01-01T18:05:02.000Z |
| 2 | 101 | 1 | | | 2020-01-01T19:00:52.000Z |
| 3 | 102 | 1 | | | 2020-01-02T12:51:23.000Z |
| 3 | 102 | 2 | | | 2020-01-02T12:51:23.000Z |
| 4 | 103 | 1 | 4 | | 2020-01-04T13:23:46.000Z |
| 4 | 103 | 1 | 4 | | 2020-01-04T13:23:46.000Z |
| 4 | 103 | 2 | 4 | | 2020-01-04T13:23:46.000Z |
| 5 | 104 | 1 | | 1 | 2020-01-08T21:00:29.000Z |
| 6 | 101 | 2 | | | 2020-01-08T21:03:13.000Z |
| 7 | 105 | 2 | | 1 | 2020-01-08T21:20:29.000Z |
| 8 | 102 | 1 | | | 2020-01-09T23:54:33.000Z |
| 9 | 103 | 1 | 4 | 1, 5 | 2020-01-10T11:22:59.000Z |
| 10 | 104 | 1 | | | 2020-01-11T18:34:49.000Z |
| 10 | 104 | 1 | 2, 6 | 1, 4 | 2020-01-11T18:34:49.000Z |
+───────────+──────────────+───────────+─────────────+─────────+───────────────────────────+
--2. runner_orders
/*
- pickup time, distance, duration is of the wrong type
- records have nulls in these columns when the orders are cancelled
- convert text 'null' to null values
- units (km, minutes) need to be removed from distance and duration
*/
DROP TABLE IF EXISTS updated_runner_orders;
CREATE TEMP TABLE updated_runner_orders AS (
SELECT
order_id,
runner_id,
CASE WHEN pickup_time LIKE 'null' THEN null ELSE pickup_time END::timestamp AS pickup_time,
NULLIF(regexp_replace(distance, '[^0-9.]','','g'), '')::numeric AS distance,
NULLIF(regexp_replace(duration, '[^0-9.]','','g'), '')::numeric AS duration,
CASE WHEN cancellation IN ('null', 'NaN', '') THEN null ELSE cancellation END AS cancellation
FROM pizza_runner.runner_orders);
SELECT * FROM updated_runner_orders;
--Result:
| order_id | runner_id | pickup_time | distance | duration | cancellation |
|----------|-----------|---------------------|----------|----------|-------------------------|
| 1 | 1 | 2020-01-01 18:15:34 | 20 | 32 | |
| 2 | 1 | 2020-01-01 19:10:54 | 20 | 27 | |
| 3 | 1 | 2020-01-02 00:12:37 | 13.4 | 20 | |
| 4 | 2 | 2020-01-04 13:53:03 | 23.4 | 40 | |
| 5 | 3 | 2020-01-08 21:10:57 | 10 | 15 | |
| 6 | 3 | | | | Restaurant Cancellation |
| 7 | 2 | 2020-01-08 21:30:45 | 25 | 25 | |
| 8 | 2 | 2020-01-10 00:15:02 | 23.4 | 15 | |
| 9 | 2 | | | | Customer Cancellation |
| 10 | 1 | 2020-01-11 18:50:20 | 10 | 10 | |
*/
+───────────+────────────+──────────────────────+───────────+───────────+──────────────────────────+
| order_id | runner_id | pickup_time | distance | duration | cancellation |
+───────────+────────────+──────────────────────+───────────+───────────+──────────────────────────+
| 1 | 1 | 2020-01-01 18:15:34 | 20 | 32 | |
| 2 | 1 | 2020-01-01 19:10:54 | 20 | 27 | |
| 3 | 1 | 2020-01-02 00:12:37 | 13.4 | 20 | |
| 4 | 2 | 2020-01-04 13:53:03 | 23.4 | 40 | |
| 5 | 3 | 2020-01-08 21:10:57 | 10 | 15 | |
| 6 | 3 | | | | Restaurant Cancellation |
| 7 | 2 | 2020-01-08 21:30:45 | 25 | 25 | |
| 8 | 2 | 2020-01-10 00:15:02 | 23.4 | 15 | |
| 9 | 2 | | | | Customer Cancellation |
| 10 | 1 | 2020-01-11 18:50:20 | 10 | 10 | |
+───────────+────────────+──────────────────────+───────────+───────────+──────────────────────────+
-- data type check
--updated_customer_orders
SELECT
table_name,
column_name,
data_type
FROM information_schema.columns
WHERE table_name = 'updated_customer_orders'
--Result:
+──────────────────────────+──────────────+──────────────────────────────+
| table_name | column_name | data_type |
+──────────────────────────+──────────────+──────────────────────────────+
| updated_customer_orders | order_id | integer |
| updated_customer_orders | customer_id | integer |
| updated_customer_orders | pizza_id | integer |
| updated_customer_orders | exclusions | character varying |
| updated_customer_orders | extras | character varying |
| updated_customer_orders | order_time | timestamp without time zone |
+──────────────────────────+──────────────+──────────────────────────────+
| table_name | column_name | data_type |
|-------------------------|-------------|-----------------------------|
| updated_customer_orders | order_id | integer |
| updated_customer_orders | customer_id | integer |
| updated_customer_orders | pizza_id | integer |
| updated_customer_orders | exclusions | character varying |
| updated_customer_orders | extras | character varying |
| updated_customer_orders | order_time | timestamp without time zone |
--updated_runner_orders
SELECT
table_name,
column_name,
data_type
FROM information_schema.columns
WHERE table_name = 'updated_runner_orders'
--Result:
+────────────────────────+──────────────+──────────────────────────────+
| table_name | column_name | data_type |
+────────────────────────+──────────────+──────────────────────────────+
| updated_runner_orders | order_id | integer |
| updated_runner_orders | runner_id | integer |
| updated_runner_orders | pickup_time | timestamp without time zone |
| updated_runner_orders | distance | numeric |
| updated_runner_orders | duration | numeric |
| updated_runner_orders | cancellation | character varying |
+────────────────────────+──────────────+──────────────────────────────+
| table_name | column_name | data_type |
|-----------------------|--------------|-----------------------------|
| updated_runner_orders | order_id | integer |
| updated_runner_orders | runner_id | integer |
| updated_runner_orders | pickup_time | timestamp without time zone |
| updated_runner_orders | distance | numeric |
| updated_runner_orders | duration | numeric |
| updated_runner_orders | cancellation | character varying |
/* --------------------
Case Study Questions:
Pizza Metrics
--------------------*/
-- 1. How many pizzas were ordered?
SELECT COUNT(*) AS pizza_count
FROM updated_customer_orders;
--Result:
+──────────────+
| pizza_count |
+──────────────+
| 14 |
+──────────────+
-- 2. How many unique customer orders were made?
SELECT COUNT (DISTINCT order_id) AS order_count
FROM updated_customer_orders;
--Result:
+──────────────+
| order_count |
+──────────────+
| 10 |
+──────────────+
-- 3. How many successful orders were delivered by each runner?
SELECT
runner_id,
COUNT(order_id) AS successful_orders
FROM updated_runner_orders
WHERE cancellation IS NULL
OR cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY runner_id
ORDER BY successful_orders DESC;
--Result:
+────────────+────────────────────+
| runner_id | successful_orders |
+────────────+────────────────────+
| 1 | 4 |
| 2 | 3 |
| 3 | 1 |
+────────────+────────────────────+
-- 4. How many of each type of pizza was delivered?
SELECT
pn.pizza_name,
COUNT(co.*) AS pizza_type_count
FROM updated_customer_orders AS co
INNER JOIN pizza_runner.pizza_names AS pn
ON co.pizza_id = pn.pizza_id
INNER JOIN pizza_runner.runner_orders AS ro
ON co.order_id = ro.order_id
WHERE cancellation IS NULL
OR cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY pn.pizza_name
ORDER BY pn.pizza_name;
--OR
SELECT
pn.pizza_name,
COUNT(co.*) AS pizza_type_count
FROM updated_customer_orders AS co
INNER JOIN pizza_runner.pizza_names AS pn
ON co.pizza_id = pn.pizza_id
WHERE EXISTS (
SELECT 1 FROM updated_runner_orders AS ro
WHERE ro.order_id = co.order_id
AND (
ro.cancellation IS NULL
OR ro.cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
)
)
GROUP BY pn.pizza_name
ORDER BY pn.pizza_name;
--Result:
+─────────────+───────────────────+
| pizza_name | pizza_type_count |
+─────────────+───────────────────+
| Meatlovers | 9 |
| Vegetarian | 3 |
+─────────────+───────────────────+
-- 5. How many Vegetarian and Meatlovers were ordered by each customer?
SELECT
customer_id,
SUM(CASE WHEN pizza_id = 1 THEN 1 ELSE 0 END) AS meat_lovers,
SUM(CASE WHEN pizza_id = 2 THEN 1 ELSE 0 END) AS vegetarian
FROM updated_customer_orders
GROUP BY customer_id;
--Result:
+──────────────+──────────────+─────────────+
| customer_id | meat_lovers | vegetarian |
+──────────────+──────────────+─────────────+
| 101 | 2 | 1 |
| 103 | 3 | 1 |
| 104 | 3 | 0 |
| 105 | 0 | 1 |
| 102 | 2 | 1 |
+──────────────+──────────────+─────────────+
-- 6. What was the maximum number of pizzas delivered in a single order?
SELECT MAX(pizza_count) AS max_count
FROM (
SELECT
co.order_id,
COUNT(co.pizza_id) AS pizza_count
FROM updated_customer_orders AS co
INNER JOIN updated_runner_orders AS ro
ON co.order_id = ro.order_id
WHERE
ro.cancellation IS NULL
OR ro.cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY co.order_id) AS mycount;
--Result:
+────────────+
| max_count |
+────────────+
| 3 |
+────────────+
-- 7. For each customer, how many delivered pizzas had at least 1 change and how many had no changes?
SELECT
co.customer_id,
SUM (CASE WHEN co.exclusions IS NOT NULL OR co.extras IS NOT NULL THEN 1 ELSE 0 END) AS changes,
SUM (CASE WHEN co.exclusions IS NULL OR co.extras IS NULL THEN 1 ELSE 0 END) AS no_change
FROM updated_customer_orders AS co
INNER JOIN updated_runner_orders AS ro
ON co.order_id = ro.order_id
WHERE ro.cancellation IS NULL
OR ro.cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY co.customer_id
ORDER BY co.customer_id;
--Result:
+──────────────+──────────+────────────+
| customer_id | changes | no_change |
+──────────────+──────────+────────────+
| 101 | 0 | 2 |
| 102 | 0 | 3 |
| 103 | 3 | 3 |
| 104 | 2 | 2 |
| 105 | 1 | 1 |
+──────────────+──────────+────────────+
-- 8. How many pizzas were delivered that had both exclusions and extras?
SELECT
SUM(CASE WHEN co.exclusions IS NOT NULL AND co.extras IS NOT NULL THEN 1 ELSE 0 END) as pizza_count
FROM updated_customer_orders AS co
INNER JOIN updated_runner_orders AS ro
ON co.order_id = ro.order_id
WHERE ro.cancellation IS NULL
OR ro.cancellation NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
--Result:
+──────────────+
| pizza_count |
+──────────────+
| 1 |
+──────────────+
-- 9. What was the total volume of pizzas ordered for each hour of the day?
SELECT
DATE_PART('hour', order_time::TIMESTAMP) AS hour_of_day,
COUNT(*) AS pizza_count
FROM updated_customer_orders
WHERE order_time IS NOT NULL
GROUP BY hour_of_day
ORDER BY hour_of_day;
--Result:
+──────────────+──────────────+
| hour_of_day | pizza_count |
+──────────────+──────────────+
| 11 | 1 |
| 12 | 2 |
| 13 | 3 |
| 18 | 3 |
| 19 | 1 |
| 21 | 3 |
| 23 | 1 |
+──────────────+──────────────+
-- 10. What was the volume of orders for each day of the week?
SELECT
TO_CHAR(order_time, 'Day') AS day_of_week,
COUNT(*) AS pizza_count
FROM updated_customer_orders
GROUP BY
day_of_week,
DATE_PART('dow', order_time)
ORDER BY day_of_week;
--Result:
+──────────────+──────────────+
| day_of_week | pizza_count |
+──────────────+──────────────+
| Friday | 1 |
| Saturday | 5 |
| Thursday | 3 |
| Wednesday | 5 |
+──────────────+──────────────+
/* --------------------
Case Study Questions:
Runner and Customer Experience
--------------------*/
-- How many runners signed up for each 1 week period? (i.e. week starts 2021-01-01)
WITH runner_signups AS (
SELECT
runner_id,
registration_date,
registration_date - ((registration_date - '2021-01-01') % 7) AS start_of_week
FROM pizza_runner.runners
)
SELECT
start_of_week,
COUNT(runner_id) AS signups
FROM runner_signups
GROUP BY start_of_week
ORDER BY start_of_week;
--Result:
+───────────────────────────+──────────+
| start_of_week | signups |
+───────────────────────────+──────────+
| 2021-01-01T00:00:00.000Z | 2 |
| 2021-01-08T00:00:00.000Z | 1 |
| 2021-01-15T00:00:00.000Z | 1 |
+───────────────────────────+──────────+
| start_of_week | signups |
|--------------------------|---------|
| 2021-01-01T00:00:00.000Z | 2 |
| 2021-01-08T00:00:00.000Z | 1 |
| 2021-01-15T00:00:00.000Z | 1 |
-- What was the average time in minutes it took for each runner to arrive at the Pizza Runner HQ to pickup the order?
WITH runner_pickups AS (
SELECT
ro.runner_id,
ro.order_id,
co.order_time,
ro.pickup_time,
(pickup_time - order_time) AS time_to_pickup
FROM updated_runner_orders AS ro
INNER JOIN updated_customer_orders AS co
ON ro.order_id = co.order_id
)
SELECT
runner_id,
date_part('minutes', AVG(time_to_pickup)) AS avg_arrival_minutes
FROM runner_pickups
GROUP BY runner_id
ORDER BY runner_id;
--Result:
+────────────+──────────────────────+
| runner_id | avg_arrival_minutes |
+────────────+──────────────────────+
| 1 | -4 |
| 2 | 23 |
| 3 | 10 |
+────────────+──────────────────────+
| runner_id | avg_arrival_minutes |
|-----------|---------------------|
| 1 | -4 |
| 2 | 23 |
| 3 | 10 |
-- Is there any relationship between the number of pizzas and how long the order takes to prepare?
WITH order_count AS (
SELECT
order_id,
order_time,
COUNT(pizza_id) AS pizzas_order_count
FROM updated_customer_orders
GROUP BY order_id, order_time
),
prepare_time AS (
SELECT
ro.order_id,
co.order_time,
ro.pickup_time,
co.pizzas_order_count,
(pickup_time - order_time) AS time_to_pickup
FROM updated_runner_orders AS ro
INNER JOIN order_count AS co
ON ro.order_id = co.order_id
WHERE pickup_time IS NOT NULL
)
SELECT
pizzas_order_count,
AVG(time_to_pickup) AS avg_time
FROM prepare_time
GROUP BY pizzas_order_count
ORDER BY pizzas_order_count;
--Result:
+─────────────────────+──────────────────+
| pizzas_order_count | avg_time |
+─────────────────────+──────────────────+
| 1 | 12 |
| 2 | -6 |
| 3 | 29 |
+─────────────────────+──────────────────+
| pizzas_order_count | avg_time |
|--------------------|-----------------|
| 1 | 12 |
| 2 | -6 |
| 3 | 29 |
-- What was the average distance travelled for each runner?
SELECT
runner_id,
ROUND(AVG(distance), 2) AS avg_distance
FROM updated_runner_orders
GROUP BY runner_id
ORDER BY runner_id;
--Result:
+────────────+───────────────+
| runner_id | avg_distance |
+────────────+───────────────+
| 1 | 15.85 |
| 2 | 23.93 |
| 3 | 10.00 |
+────────────+───────────────+
| runner_id | avg_distance |
|-----------|--------------|
| 1 | 15.85 |
| 2 | 23.93 |
| 3 | 10.00 |
-- What was the difference between the longest and shortest delivery times for all orders?
SELECT
MAX(duration) - MIN(duration) AS difference
FROM updated_runner_orders;
--Result:
+─────────────+
| difference |
+─────────────+
| 30 |
+─────────────+
| difference |
|------------|
| 30 |
-- What was the average speed for each runner for each delivery and do you notice any trend for these values?
WITH order_count AS (
SELECT
order_id,
order_time,
COUNT(pizza_id) AS pizzas_count
FROM updated_customer_orders
GROUP BY
order_id,
order_time
)
SELECT
ro.order_id,
ro.runner_id,
co.pizzas_count,
ro.distance,
ro.duration,
ROUND(60 * ro.distance / ro.duration, 2) AS speed
FROM updated_runner_orders AS ro
INNER JOIN order_count AS co
ON ro.order_id = co.order_id
WHERE pickup_time IS NOT NULL
ORDER BY speed DESC
--Result:
+───────────+────────────+───────────────+───────────+───────────+────────+
| order_id | runner_id | pizzas_count | distance | duration | speed |
+───────────+────────────+───────────────+───────────+───────────+────────+
| 8 | 2 | 1 | 23.4 | 15 | 93.60 |
| 7 | 2 | 1 | 25 | 25 | 60.00 |
| 10 | 1 | 2 | 10 | 10 | 60.00 |
| 2 | 1 | 1 | 20 | 27 | 44.44 |
| 3 | 1 | 2 | 13.4 | 20 | 40.20 |
| 5 | 3 | 1 | 10 | 15 | 40.00 |
| 1 | 1 | 1 | 20 | 32 | 37.50 |
| 4 | 2 | 3 | 23.4 | 40 | 35.10 |
+───────────+────────────+───────────────+───────────+───────────+────────+
| order_id | runner_id | pizzas_count | distance | duration | speed |
|----------|-----------|--------------|----------|----------|-------|
| 8 | 2 | 1 | 23.4 | 15 | 93.60 |
| 7 | 2 | 1 | 25 | 25 | 60.00 |
| 10 | 1 | 2 | 10 | 10 | 60.00 |
| 2 | 1 | 1 | 20 | 27 | 44.44 |
| 3 | 1 | 2 | 13.4 | 20 | 40.20 |
| 5 | 3 | 1 | 10 | 15 | 40.00 |
| 1 | 1 | 1 | 20 | 32 | 37.50 |
| 4 | 2 | 3 | 23.4 | 40 | 35.10 |
/*Finding:
Orders shown in decreasing order of average speed:
While the fastest order only carried 1 pizza and the slowest order carried 3 pizzas,
there is no clear trend that more pizzas slow down the delivery speed of an order.
*/
-- What is the successful delivery percentage for each runner?
SELECT
runner_id,
COUNT(pickup_time) as delivered,
COUNT(order_id) AS total,
ROUND(100 * COUNT(pickup_time) / COUNT(order_id)) AS delivery_percent
FROM updated_runner_orders
GROUP BY runner_id
ORDER BY runner_id;
--Result:
+────────────+────────────+────────+─────────────────+
| runner_id | delivered | total | delivery_percent|
+────────────+────────────+────────+─────────────────+
| 1 | 4 | 4 | 100 |
| 2 | 3 | 4 | 75 |
| 3 | 1 | 2 | 50 |
+────────────+────────────+────────+─────────────────+
| runner_id | delivered | total | delivery_percent |
|-----------|-----------|-------|------------------|
| 1 | 4 | 4 | 100 |
| 2 | 3 | 4 | 75 |
| 3 | 1 | 2 | 50 |