The asset_status_log table records state changes of industrial assets. Each row marks the moment an asset switched to the given status, which is either 'RUNNING' or 'DOWN'.
An asset is down from a DOWN row until its next RUNNING row (by status_time). Consecutive DOWN rows for the same asset belong to the same down period and must not be double-counted. A DOWN period with no later RUNNING row is still ongoing and is counted up to the latest status_time in the entire table.
Write a query that returns each asset_id together with total_downtime_minutes: the total downtime across all of the asset's down periods, computed in seconds and then converted to whole minutes rounded down. Include only assets that have at least one DOWN row. Use a window function (LEAD) for this; do not use a self-join.
Input Table(s)
asset_status_log
| Column | Type | Meaning |
|---|
| log_id | INT | Primary key of the log row |
| asset_id | INT | Asset whose state changed (NOT NULL) |
| status | VARCHAR(10) | New state of the asset, 'RUNNING' or 'DOWN' (NOT NULL) |
| status_time | TIMESTAMP | Moment the asset switched to status, stored in whole seconds (NOT NULL) |
Keys: log_id is the primary key. No two rows for the same asset_id share the same status_time.
Output Contract
| Column | Type | Meaning |
|---|
| asset_id | INT | An asset with at least one DOWN row |
| total_downtime_minutes | BIGINT | Total down seconds across all of the asset's down periods, divided by 60 and floored |
-
One row per asset that has at least one
DOWN
row.
-
Sum the seconds across all down periods first, then floor to whole minutes once.
-
Order by
total_downtime_minutes
descending, then
asset_id
ascending.
Example
asset_status_log
| log_id | asset_id | status | status_time |
|---|
| 1 | 1 | DOWN | 2024-01-01 08:00:00 |
| 2 | 1 | DOWN | 2024-01-01 08:20:00 |
| 3 | 1 | RUNNING | 2024-01-01 09:00:30 |
| 4 | 2 | RUNNING | 2024-01-01 07:00:00 |
| 5 | 2 | DOWN | 2024-01-01 09:30:00 |
| 6 | 3 | RUNNING | 2024-01-01 08:00:00 |
| 7 | 1 | DOWN | 2024-01-01 09:50:00 |
| 8 | 1 | RUNNING | 2024-01-01 09:55:30 |
| 9 | 3 | RUNNING | 2024-01-01 10:00:00 |
| 10 | 4 | DOWN | 2024-01-01 09:00:00 |
| 11 | 4 | RUNNING | 2024-01-01 09:30:00 |
Expected result:
| asset_id | total_downtime_minutes |
|---|
| 1 | 66 |
| 2 | 30 |
| 4 | 30 |
Asset 1 has two down periods: 08:00:00 to 09:00:30 (3,630 seconds, with the second DOWN row at 08:20:00 inside it) and 09:50:00 to 09:55:30 (330 seconds). The total is 3,960 seconds, which is exactly 66 minutes. Asset 2 went down at 09:30:00 and never came back, so its period runs to 10:00:00, the latest status_time in the table (log 9, which belongs to asset 3): 1,800 seconds, or 30 minutes. Asset 4 was down for 30 minutes. Asset 3 never went down and is left out. Assets 2 and 4 tie at 30, so asset 2 comes first.
Constraints and Clarifications
-
The table has 1 to 10,000 rows, and
log_id
is unique.
-
No column is NULL.
status
is always
'RUNNING'
or
'DOWN'
.
-
No two rows for the same asset share a
status_time
, so each asset's rows have one clear order.
-
A
RUNNING
row that follows another
RUNNING
row adds no downtime.
-
An ongoing period ends at the latest
status_time
across all assets, not the asset's own latest row. A
DOWN
row that is itself at that latest time adds 0 seconds, but its asset still appears in the result.
-
Downtime is summed in seconds before being floored to whole minutes.