64
64
ELSE cancellation
65
65
END;
66
66
67
-
68
-
69
67
-- c) Clean exclusions and extras columns
70
68
UPDATE pizza_runner .customer_orders
71
69
SET exclusions = CASE
@@ -79,7 +77,6 @@ SET extras = CASE
79
77
ELSE extras
80
78
END;
81
79
82
-
83
80
-- d) Change `pickup_time` to `DATETIME`
84
81
ALTER TABLE pizza_runner .runner_orders
85
82
ALTER COLUMN pickup_time DATETIME;
@@ -94,6 +91,8 @@ ALTER COLUMN duration INT;
94
91
95
92
-- - START
96
93
94
+ -- - A. Pizza Metrics
95
+
97
96
-- Question 1) How many pizzas were ordered?
98
97
99
98
SELECT
@@ -132,3 +131,84 @@ GROUP BY
132
131
b .pizza_name ;
133
132
134
133
-- Question 5) How many Vegetarian and Meatlovers were ordered by each customer?
134
+
135
+ SELECT
136
+ a .customer_id ,
137
+ b .pizza_name ,
138
+ COUNT (a .pizza_id ) AS count_of_pizza
139
+ FROM
140
+ pizza_runner .customer_orders AS a
141
+ JOIN pizza_runner .pizza_names AS b ON b .pizza_id = a .pizza_id
142
+ GROUP BY
143
+ a .customer_id ,
144
+ b .pizza_name
145
+
146
+ -- Question 6) What was the maximum number of pizzas delivered in a single order?
147
+
148
+ SELECT
149
+ MAX (count_of_pizza) AS max_pizza_in_single_order
150
+ FROM (
151
+ SELECT
152
+ order_id,
153
+ COUNT (pizza_id) AS count_of_pizza
154
+ FROM
155
+ pizza_runner .customer_orders
156
+ GROUP BY
157
+ order_id
158
+ ) AS subquery;
159
+
160
+ -- Question 7) For each customer, how many delivered pizzas had at least 1 change and how many had no changes?
161
+
162
+
163
+ WITH order_changes AS (
164
+
165
+ SELECT
166
+ customer_id,
167
+ COUNT (CASE WHEN exclusions IS NOT NULL OR extras IS NOT NULL THEN 1 END) AS pizzas_with_changes,
168
+ COUNT (CASE WHEN exclusions IS NULL AND extras IS NULL THEN 1 END) AS pizzas_without_changes
169
+ FROM
170
+ pizza_runner .customer_orders
171
+ GROUP BY
172
+ customer_id
173
+ )
174
+ SELECT
175
+ customer_id,
176
+ pizzas_with_changes,
177
+ pizzas_without_changes
178
+ FROM
179
+ order_changes
180
+ ORDER BY
181
+ customer_id;
182
+
183
+ -- Question 8) How many pizzas were delivered that had both exclusions and extras?
184
+
185
+ -- Solution I
186
+
187
+ SELECT
188
+ COUNT (* ) AS count_of_pizza
189
+
190
+ FROM (
191
+ SELECT
192
+ order_id,
193
+ pizza_id,
194
+ COUNT (CASE WHEN exclusions IS NOT NULL AND extras IS NOT NULL THEN 1 END) AS exclusions_with_extras
195
+ FROM
196
+ pizza_runner .customer_orders
197
+ GROUP BY
198
+ order_id, pizza_id
199
+ ) AS subquery
200
+
201
+ WHERE
202
+ exclusions_with_extras != 0
203
+
204
+
205
+ -- Solution II
206
+
207
+ SELECT
208
+ COUNT (* ) AS count_of_pizza
209
+ FROM
210
+ pizza_runner .customer_orders
211
+ WHERE
212
+ exclusions IS NOT NULL AND extras IS NOT NULL
213
+
214
+ -- Question 9) What was the total volume of pizzas ordered for each hour of the day?
0 commit comments