
delivery_id is the column of unique values of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
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.
This was my query but its wrong:
Could I get some help? Thank you very much.
WITH CTE AS
(SELECT DISTINCT customer_id, sum(if(datediff(customer_pref_delivery_date, order_date) = 0, 1, 0)) as immediate
FROM Delivery
GROUP BY customer_id)
SELECT ROUND(100*sum(immediate) / sum(customer_id), 2) as immediate_percentage FROM CTE