Problem: 1174. 即时食物配送 II
👨🏫 参考题解
Code
-- 计算立即配送的订单百分比
select round (
-- 计算订单日期与客户偏好配送日期相同的订单数量
sum(case when order_date = customer_pref_delivery_date then 1 else 0 end) * 100 /
-- 计算总订单数量
count(*),
2
) as immediate_percentage
from Delivery
where (customer_id, order_date) in (
-- 选择每个客户的最早订单日期
select customer_id, min(order_date)
from Delivery
-- 按客户ID分组
group by customer_id
)