Skip to content

Commit 08ef1b3

Browse files
authored
Add files via upload
1 parent 7d32e57 commit 08ef1b3

File tree

1 file changed

+83
-3
lines changed

1 file changed

+83
-3
lines changed

Projects/ 8_Week_SQL_Challenge/Case_Study_2/Solution.sql

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ SET
6464
ELSE cancellation
6565
END;
6666

67-
68-
6967
-- c) Clean exclusions and extras columns
7068
UPDATE pizza_runner.customer_orders
7169
SET exclusions = CASE
@@ -79,7 +77,6 @@ SET extras = CASE
7977
ELSE extras
8078
END;
8179

82-
8380
-- d) Change `pickup_time` to `DATETIME`
8481
ALTER TABLE pizza_runner.runner_orders
8582
ALTER COLUMN pickup_time DATETIME;
@@ -94,6 +91,8 @@ ALTER COLUMN duration INT;
9491

9592
--- START
9693

94+
--- A. Pizza Metrics
95+
9796
-- Question 1) How many pizzas were ordered?
9897

9998
SELECT
@@ -132,3 +131,84 @@ GROUP BY
132131
b.pizza_name;
133132

134133
-- 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

Comments
 (0)