Skip to content

Commit c188739

Browse files
authored
Add files via upload
1 parent 59ab124 commit c188739

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

Projects/ 8_Week_SQL_Challenge/Case_Study_2/Solution.sql

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,119 @@ Select or clear the Prevent saving changes that require the table re-creation ch
1616

1717
USE pizza_runner;
1818

19-
SELECT
20-
*
21-
FROM
22-
pizza_runner.pizza_recipes;
19+
20+
--- Before working in SQL, must be clean column and must be prepare tables for analysis.
21+
22+
-- 1) Correcting the NULL and empty string ('') values in the columns to be NULL
23+
Select * from pizza_runner.customer_orders;
24+
select DISTINCT exclusions from pizza_runner.customer_orders;
25+
select DISTINCT extras from pizza_runner.customer_orders;
26+
27+
select order_id,
28+
customer_id,
29+
pizza_id,
30+
case when exclusions = 'null' or exclusions = '' then NULL else exclusions END as exclusions,
31+
case when extras = 'null' or extras = '' then NULL else extras END as extras,
32+
order_time
33+
FROM pizza_runner.customer_orders;
34+
35+
-- 2)Clean pickup_time, distance, duration, and cancellation columns
36+
37+
-- a) Update invalid pickup_time values to NULL
38+
UPDATE pizza_runner.runner_orders
39+
SET pickup_time = NULL
40+
WHERE ISDATE(pickup_time) = 0;
41+
42+
43+
-- b)Correction of km, min, mins, minute, minutes expressions found in columns
44+
UPDATE pizza_runner.runner_orders
45+
SET
46+
pickup_time = CASE
47+
WHEN pickup_time = 'null' THEN NULL
48+
ELSE pickup_time
49+
END,
50+
distance = CASE
51+
WHEN distance = 'null' THEN NULL
52+
WHEN distance LIKE '%km' THEN REPLACE(distance, 'km', '')
53+
ELSE distance
54+
END,
55+
duration = CASE
56+
WHEN duration = 'null' THEN NULL
57+
WHEN duration LIKE '%mins' THEN REPLACE(duration, 'mins', '')
58+
WHEN duration LIKE '%minute' THEN REPLACE(duration, 'minute', '')
59+
WHEN duration LIKE '%minutes' THEN REPLACE(duration, 'minutes', '')
60+
ELSE duration
61+
END,
62+
cancellation = CASE
63+
WHEN cancellation = 'null' OR cancellation = '' THEN NULL
64+
ELSE cancellation
65+
END;
66+
67+
68+
69+
-- c) Clean exclusions and extras columns
70+
UPDATE pizza_runner.customer_orders
71+
SET exclusions = CASE
72+
WHEN exclusions = 'null' OR exclusions = '' THEN NULL
73+
ELSE exclusions
74+
END;
75+
76+
UPDATE pizza_runner.customer_orders
77+
SET extras = CASE
78+
WHEN extras = 'null' OR extras = '' THEN NULL
79+
ELSE extras
80+
END;
81+
82+
83+
-- d) Change `pickup_time` to `DATETIME`
84+
ALTER TABLE pizza_runner.runner_orders
85+
ALTER COLUMN pickup_time DATETIME;
86+
87+
-- e) Change `distance` to `FLOAT`
88+
ALTER TABLE pizza_runner.runner_orders
89+
ALTER COLUMN distance FLOAT;
90+
91+
-- f) Change `duration` to `INT`
92+
ALTER TABLE pizza_runner.runner_orders
93+
ALTER COLUMN duration INT;
94+
95+
--- START
2396

2497
-- Question 1) How many pizzas were ordered?
2598

2699
SELECT
27100
COUNT(order_id) AS count_of_pizzas
28101
FROM
29102
pizza_runner.customer_orders;
30-
--Answer = 14
31103

32104
-- Question 2) How many unique customer orders were made?
33105

34106
SELECT
35107
COUNT(DISTINCT A.customer_id) AS count_of_customer
36108
FROM
37109
pizza_runner.customer_orders AS A;
38-
--Answer = 5
39-
40110

41111
-- Question 3) How many successful orders were delivered by each runner?
42112

43113
SELECT
44114
runner_id,
45-
COUNT(*) AS succesful_orders
115+
COUNT(*) AS successful_orders
46116
FROM
47117
pizza_runner.runner_orders
118+
WHERE
119+
cancellation IS NULL
120+
GROUP BY
121+
runner_id;
122+
123+
-- Question 4) How many of each type of pizza was delivered?
124+
125+
SELECT
126+
b.pizza_name,
127+
COUNT(a.order_id) AS delivered_count
128+
FROM
129+
pizza_runner.customer_orders AS a
130+
JOIN pizza_runner.pizza_names AS b ON a.pizza_id = b.pizza_id
131+
GROUP BY
132+
b.pizza_name;
133+
134+
-- Question 5) How many Vegetarian and Meatlovers were ordered by each customer?

0 commit comments

Comments
 (0)