[코드카타] SQL 36 Immediate Food Delivery II

Data_Student·2024년 11월 18일
0

코드카타

목록 보기
45/57

[코드카타] SQL 36 Immediate Food Delivery II

36. Immediate Food Delivery II
https://leetcode.com/problems/immediate-food-delivery-ii/description/

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 cnt as (
    select customer_id, order_date, 
    if(min(order_date) = min(customer_pref_delivery_date), 'immediate', 'scheduled') state
from Delivery
group by customer_id
)
select round(count(case when state = 'immediate' then 1 end)/count(*)*100, 2) immediate_percentage
from Delivery d join cnt c on d.customer_id=c.customer_id and d.order_date = c.order_date

0개의 댓글