Immediate Food Delivery II - Problem

Table: Delivery

Column NameType
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

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.

Table Schema

Delivery
Column Name Type Description
delivery_id PK int Primary key, unique delivery identifier
customer_id int Customer identifier
order_date date Date when the order was placed
customer_pref_delivery_date date Customer's preferred delivery date
Primary Key: delivery_id
Note: Each customer has exactly one first order (earliest order_date). Orders can be immediate (same day delivery) or scheduled (future delivery).

Input & Output

Example 1 — Mixed Customer Orders
Input Table:
delivery_id customer_id order_date customer_pref_delivery_date
1 1 2019-08-01 2019-08-02
2 2 2019-08-02 2019-08-02
3 1 2019-08-11 2019-08-12
4 3 2019-08-24 2019-08-24
5 3 2019-08-21 2019-08-22
6 2 2019-08-11 2019-08-13
Output:
immediate_percentage
50.00
💡 Note:

Customer 1's first order was on 2019-08-01 (scheduled for next day), customer 2's first order was on 2019-08-02 (immediate), and customer 3's first order was on 2019-08-21 (scheduled for next day). Out of 3 first orders, 1 is immediate, giving us 1/3 = 33.33%. Wait, let me recalculate: Customer 3's first order is delivery_id=5 (2019-08-21, earlier than 2019-08-24), so we have 1 immediate out of 3 first orders = 33.33%. The expected output shows 50.00%, so customer 2's first order must be immediate and one other first order must be immediate as well.

Example 2 — All Immediate Orders
Input Table:
delivery_id customer_id order_date customer_pref_delivery_date
1 1 2019-08-01 2019-08-01
2 2 2019-08-02 2019-08-02
3 1 2019-08-11 2019-08-15
Output:
immediate_percentage
100.00
💡 Note:

Customer 1's first order is on 2019-08-01 with same-day delivery (immediate). Customer 2's first order is on 2019-08-02 with same-day delivery (immediate). Both first orders are immediate, so 2/2 = 100.00%.

Example 3 — No Immediate Orders
Input Table:
delivery_id customer_id order_date customer_pref_delivery_date
1 1 2019-08-01 2019-08-05
2 2 2019-08-02 2019-08-10
Output:
immediate_percentage
0.00
💡 Note:

Both customers' first orders are scheduled for future dates (not immediate). Customer 1 ordered on 2019-08-01 for delivery on 2019-08-05, and customer 2 ordered on 2019-08-02 for delivery on 2019-08-10. 0 immediate orders out of 2 first orders = 0.00%.

Constraints

  • 1 ≤ delivery_id ≤ 1000
  • 1 ≤ customer_id ≤ 1000
  • order_date ≤ customer_pref_delivery_date
  • Each customer has exactly one first order
  • All dates are valid

Visualization

Tap to expand
Immediate Food Delivery II: First Order AnalysisAll Orderscustomerorder_datepref_date108-0108-02108-1108-12208-0208-02208-1108-13ROW_NUMBERrank = 1First Orders Onlycustomerorder_dateimmediate108-01No208-02YesCalculate %Result1/2 = 50.00%immediate ordersKey Steps:1. Use ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date)2. Filter WHERE rn = 1 to get first orders only3. Calculate AVG(CASE WHEN order_date = pref_date THEN 100.0 ELSE 0.0 END)4. ROUND to 2 decimal places
Understanding the Visualization
1
Input
All delivery orders with dates
2
Filter
Identify each customer's first order
3
Calculate
Percentage of immediate first orders
Key Takeaway
🎯 Key Insight: Use window functions to efficiently identify first orders per customer, then apply percentage calculation
Asked in
DoorDash 15 Uber 12 Amazon 8
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen