-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8weekssql.sql
More file actions
121 lines (99 loc) · 3.52 KB
/
8weekssql.sql
File metadata and controls
121 lines (99 loc) · 3.52 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
-- A. Pizza Metrics
-- 1. How many pizzas were ordered?
SELECT
COUNT(*) AS pizzas_count
FROM customer_orders;
-- 2. How many unique customer orders were made?
SELECT
COUNT(DISTINCT order_id) AS orders_count
FROM customer_orders;
-- 3. How many successful orders were delivered by each runner?
SELECT
runner_id,
COUNT(order_id) AS successful_orders_count
FROM runner_orders
WHERE cancallation IS NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY runner_id
ORDER BY 2;
-- 4. How many of each type of pizza was delivered?
SELECT
c.pizza_id,
COUNT(c.pizza_id) AS pizzas_delivered
FROM customer_orders c
JOIN runner_orders r
ON c.order_id = r.order_id
WHERE r.cancallation IS NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY c.pizza_id
ORDER BY 2;
-- 5. How many Vegetarian and Meatlovers were ordered by each customer?
SELECT
c.customer_id,
SUM(CASE WHEN p.pizza_name = 'Vegetarian' THEN 1 ELSE 0 END) AS Vegetarian,
SUM(CASE WHEN p.pizza_name = 'Meatlover' THEN 1 ELSE 0 END) AS Meatlover
FROM customer_orders c
JOIN pizza_names p
ON c.pizza_id = p.pizza_id
GROUP BY c.customer_id;
-- 6. What was the maximum number of pizzas delivered in a single order?
WITH maximum AS (
SELECT
c.order_id,
COUNT(c.pizza_id) AS orders_amount
FROM customer_orders c
JOIN runner_orders r
ON c.order_id = r.order_id
WHERE r.cancallation IS NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY c.order_id)
SELECT
MAX(orders_amount)
FROM maximum;
-- 7. For each customer, how many delivered pizzas had at least 1 change and how many had no changes?
WITH customer_orders_cleaned AS(
SELECT
order_id,
customer_id,
pizza_id,
CASE WHEN exclusions IN ('null', '') THEN NULL ELSE exclusions END AS exclusions,
CASE WHEN extras IN ('Nan', 'null', '') THEN NULL ELSE extras END AS extras
FROM customer_orders)
SELECT
c.customer_id,
SUM(CASE WHEN c.exclusions IS NOT NULL OR c.extras IS NOT NULL THEN 1 ELSE 0 END) AS at_leat_1_change,
SUM(CASE WHEN c.exclusions IS NULL AND c.extras IS NULL THEN 1 ELSE 0 END) AS no_changes
FROM customer_orders_cleaned c
JOIN runner_orders r
ON c.order_id = r.order_id
WHERE r.cancallation IS NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
GROUP BY c.customer_id;
-- 8. How many pizzas were delivered that had both exclusions and extras?
WITH customer_orders_cleaned AS(
SELECT
order_id,
customer_id,
pizza_id,
CASE WHEN exclusions IN ('null', '') THEN NULL ELSE exclusions END AS exclusions,
CASE WHEN extras IN ('Nan', 'null', '') THEN NULL ELSE extras END AS extras
FROM customer_orders),
exclusions_and_extras AS(
SELECT
order_id,
pizza_id,
CASE WHEN exclusions IS NOT NULL AND extras IS NOT NULL THEN 1 ELSE 0 END AS excl_and_extr
FROM customer_orders_cleaned)
SELECT
COUNT(ee.pizza_id) AS pizzas_delivered
FROM exclusions_and_extras ee
JOIN runner_orders r
ON ee.order_id = r.order_id
WHERE r.cancallation IS NOT IN ('Restaurant Cancellation', 'Customer Cancellation')
AND ee.excl_and_extr = 1;
-- 9. What was the total volume of pizzas ordered for each hour of the day?
SELECT
DATE_PART('hour', r.pickup_time) AS hour_of_day,
COUNT(c.pizza_id) AS pizza_count
FROM runner_orders r
JOIN customer_orders c
ON r.order_id = c.order_id
WHERE r.pickup_time != NULL
GROUP BY r.hour_of_day
ORDER BY r.hour_of_day;