Calculate total downtime minutes per asset from a status change log

Quick Overview

Write a SQL query that totals each asset's downtime in whole minutes from a log of RUNNING and DOWN status changes. It tests the LEAD window function, adding up consecutive DOWN rows without double counting, closing ongoing periods at the table's latest timestamp, and flooring only after summing.

Calculate total downtime minutes per asset from a status change log

Company: AVEVA

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

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. ```hint Where each interval ends Measure from each `DOWN` row to the same asset's next row of any status, then check whether those intervals can ever overlap. ``` ### 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.

Overview: Write a SQL query that totals each asset's downtime in whole minutes from a log of RUNNING and DOWN status changes. It tests the LEAD window function, adding up consecutive DOWN rows without double counting, closing ongoing periods at the table's latest timestamp, and flooring only after summing.

AVEVA logo
AVEVA
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
log_idINTPrimary key of the log row
asset_idINTAsset whose state changed (NOT NULL)
statusVARCHAR(10)New state of the asset, 'RUNNING' or 'DOWN' (NOT NULL)
status_timeTIMESTAMPMoment 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

ColumnTypeMeaning
asset_idINTAn asset with at least one DOWN row
total_downtime_minutesBIGINTTotal 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_idasset_idstatusstatus_time
11DOWN2024-01-01 08:00:00
21DOWN2024-01-01 08:20:00
31RUNNING2024-01-01 09:00:30
42RUNNING2024-01-01 07:00:00
52DOWN2024-01-01 09:30:00
63RUNNING2024-01-01 08:00:00
71DOWN2024-01-01 09:50:00
81RUNNING2024-01-01 09:55:30
93RUNNING2024-01-01 10:00:00
104DOWN2024-01-01 09:00:00
114RUNNING2024-01-01 09:30:00

Expected result:

asset_idtotal_downtime_minutes
166
230
430

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.