The orders table records when each order was placed and when its fulfillment center finished it. For each fulfillment_center, compute the average processing time between order_placed and order_fulfilled in hours, reported as avg_processing_hours and rounded to 2 decimals. Only fulfilled orders count, meaning rows where order_fulfilled IS NOT NULL. Order the result by fulfillment_center ascending.
An order's processing time is the number of complete hours between the two timestamps. Leftover minutes are dropped before averaging, so an order that took 2 hours and 59 minutes counts as 2 hours.
Input Table(s)
Table orders
| Column | Type | Meaning |
|---|
| order_id | INT | Order identifier |
| fulfillment_center | VARCHAR(100) | Center that handles the order, never NULL |
| order_placed | TIMESTAMP | When the order was placed, never NULL |
| order_fulfilled | TIMESTAMP | When the order was fulfilled, NULL while the order is still open |
Primary key: order_id.
Output Contract
-
Columns, in this order:
fulfillment_center
,
avg_processing_hours
.
-
One row per fulfillment center that has at least one fulfilled order.
-
avg_processing_hours
is the mean of the per-order complete hours, rounded to 2 decimals.
-
Rows sorted by
fulfillment_center
ascending. Each center appears once, so this order is unique.
Example
Input orders:
| order_id | fulfillment_center | order_placed | order_fulfilled |
|---|
| 1 | Center-A | 2024-01-01 08:00:00 | 2024-01-01 20:00:00 |
| 2 | Center-A | 2024-01-02 10:00:00 | 2024-01-03 10:00:00 |
| 3 | Center-B | 2024-01-01 06:00:00 | 2024-01-01 12:00:00 |
| 4 | Center-B | 2024-01-03 14:00:00 | 2024-01-04 14:00:00 |
| 5 | Center-A | 2024-01-04 09:00:00 | NULL |
Expected output:
| fulfillment_center | avg_processing_hours |
|---|
| Center-A | 18.00 |
| Center-B | 15.00 |
Center-A has two fulfilled orders that took 12 and 24 hours, so its average is 18. Order 5 has no fulfillment time and is left out. Center-B's orders took 6 and 24 hours, for an average of 15.
Constraints and Clarifications
-
The table has at most 10,000 rows.
-
When
order_fulfilled
is not NULL it is greater than or equal to
order_placed
, so no duration is negative. An order fulfilled at the moment it was placed counts as 0 hours.
-
A center whose orders are all still open does not appear in the output.
-
The average of whole hours can still be fractional: two orders of 2 and 3 hours average to 2.50.
-
order_id
is unique, so no order is counted twice.
-
fulfillment_center
is a non-empty string.