A food delivery service stores one row per order in the Delivery table. An order is immediate when customer_pref_delivery_date equals order_date; otherwise it is scheduled. Write a query that returns the percentage of immediate orders in Delivery, rounded to 2 decimal places, as immediate_percentage.
Input Table(s)
Table Delivery
| Column | Type | Meaning |
|---|
| delivery_id | INT | Id of the order |
| customer_id | INT | Customer who placed the order (NOT NULL) |
| order_date | DATE | Date the order was placed (NOT NULL) |
| customer_pref_delivery_date | DATE | Delivery date the customer asked for (NOT NULL) |
Primary key: delivery_id.
Output Contract
-
Exactly one row with one column,
immediate_percentage
.
-
The value is 100 times the number of immediate orders divided by the total number of orders, rounded to 2 decimal places (for example 42.86, or 50.00 when exactly half the orders are immediate).
-
The result is a single row, so no ordering is needed.
Example
Input Delivery:
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
|---|
| 1 | 101 | 2024-03-01 | 2024-03-01 |
| 2 | 102 | 2024-03-01 | 2024-03-03 |
| 3 | 101 | 2024-03-04 | 2024-03-05 |
| 4 | 103 | 2024-03-04 | 2024-03-04 |
| 5 | 104 | 2024-03-06 | 2024-03-09 |
| 6 | 102 | 2024-03-07 | 2024-03-07 |
| 7 | 105 | 2024-03-08 | 2024-03-10 |
Expected output:
| immediate_percentage |
|---|
| 42.86 |
Orders 1, 4 and 6 have matching dates, so 3 of the 7 orders are immediate. 3 divided by 7 is 0.428571..., which is 42.857... percent and rounds to 42.86.
Constraints and Clarifications
-
Delivery
has between 1 and 10,000 rows, so the total is never zero.
-
No column is NULL, so every order is either immediate or scheduled.
-
delivery_id
is unique, so each row is one order. A customer can appear on many rows, and every one of those orders counts.
-
Any difference between the two dates makes the order scheduled.
-
When the exact percentage ends in 5 at the third decimal place, round away from zero, which is what PostgreSQL
ROUND
does on a
numeric
value.