
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ๋ฌธ์
https://leetcode.com/problems/immediate-food-delivery-ii/description/
Immediate Food Delivery II - LeetCode
Can you solve this real interview question? Immediate Food Delivery II - Table: Delivery +-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | d
leetcode.com
Table : Delivery

If the customer's preferred delivery date is the same as the order date, then the order is called immediate;
otherwise, it is called scheduled.
The first order of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has precisely one first order.
Write a solution to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.
( โฆ์ค๋ต )
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ฒซ ์ฃผ๋ฌธ ์ด๋ ฅ์ ํด๋นํ๋ ๊ณ ๊ฐ์ค ์ ํธํ๋ ๋ฐฐ์ก ์ผ์์ ์ฃผ๋ฌธ์ ๋ฐ์ '์ฆ์ ์ฃผ๋ฌธ'์ ํด๋นํ๋ ๊ณ ๊ฐ์ ๋น์จ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
ยท ์ ์ฒด๊ฐ ๋ ์ฒซ ์ฃผ๋ฌธ์ด์๋ ๋ฐ์ดํฐ๋ค์ with์ ๋ก ์์ ํ ์ด๋ธ์ ์ฌ์ฉํด์ ์ถ๋ ฅํด์คฌ๋ค.
- ๊ณ ๊ฐ๋ณ ์ ์ผ '์ฒซ๋ฒ์งธ ์ฃผ๋ฌธ' ์กฐ๊ฑด์ ์ง๊ณํจ์ MIN์ ์ฌ์ฉํ์ฌ WHERE์ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉ.
ยท CASE์ ์ ์ฌ์ฉํด์ '์ฆ์ ์ฃผ๋ฌธ'(customer_pref = customer_pref_delivery_date)์ด๋ผ๋ฉด 1 ์๋๋ฉด 0์ ๋ถ์ฌํ๊ณ
SUM๊ฐ์ผ๋ก ๋น์จ์ ํด๋นํ๋ ๊ณ ๊ฐ์๋ฅผ ๊ตฌํ๋ค.
ยท ๋ถ๋ชจ COUNT(customer_id) ์์ ๋ ord_1st ์์ ์ด๋ฏธ ๊ณ ๊ฐ๋ณ ์ฒซ๋ฒ์งธ ์ฃผ๋ฌธ์ด๋ ฅ์ ๋ฝ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ์ค๋ณต๊ฐ์ด ์์ผ๋ฏ๋ก DISTINCT๋ ์ ์ฉํ์ง ์์๋ค.
WITH ord_1st AS (
SELECT delivery_id, customer_id, order_date, customer_pref_delivery_date
FROM Delivery D1
WHERE order_date = (SELECT MIN(order_date)
FROM Delivery
WHERE customer_id = D1.customer_id
GROUP BY customer_id)
)
SELECT ROUND(
SUM(CASE WHEN order_date = customer_pref_delivery_date THEN 1 ELSE 0 END)
/ COUNT(customer_id) * 100
, 2) AS immediate_percentage
FROM ord_1st
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet code] 1341. Movie Rating (1) | 2023.09.22 |
---|---|
[Leet code] 1193. Monthly Transactions I (0) | 2023.09.21 |
[Leet code] 1141. User Activity for the Past 30 Days IEasy474634 (0) | 2023.09.13 |
[Leet code] 610. Triangle Judgement (0) | 2023.09.13 |
[Leet code] 1050. Actors and Directors Who Cooperated At Least Three Times (2) | 2023.09.08 |