runtime
117 / 139

task Gaps & islands · hard

Merged maintenance downtime

Find completed maintenance. Merge overlapping [started_at, completed_at) jobs per device. Return device_id, window_start, window_end, jobs, and downtime_hours (window length in hours, rounded to 1 decimal).

what to return

Columns
device_idwindow_startwindow_endjobsdowntime_hours in this order
Rows
146 rows · any order
Skill
Avoid double-counting overlapping intervals. Refresher: Sessionize an event stream →

expected result

expected result146 rows · any order
device_idwindow_startwindow_endjobsdowntime_hours
12025-04-10 13:00:002025-04-10 14:43:0021.7
32025-04-24 17:00:002025-04-24 18:00:0011
42025-04-23 20:00:002025-04-23 21:27:0011.4

first 3 of 146 rows

data you can query

maintenance7 columns · 168 rows
maintenance_idnumber · unique iddevice_idnumber · links to devicesmaintenance_typetexttechniciantextstarted_atdate & timecompleted_atdate & timecostdecimal
114calibrationMarco Clark2025-03-04 15:00:002025-03-04 15:57:0097.02
2102calibrationMohammed Okafor2025-03-05 16:00:002025-03-05 16:29:0097.13
3172calibrationAndre Hayes2025-03-06 16:00:002025-03-06 17:25:0067.12
4110repairPaul Turner2025-03-07 08:00:002025-03-07 10:00:00425.28
5146calibrationMohammed Okafor2025-03-07 19:00:002025-03-07 19:24:0049.06
631repairDaniela Torres2025-03-07 22:00:002025-03-08 00:00:00542.32
7132repairDaniela Torres2025-03-08 11:00:002025-03-08 14:00:00378.39
8132repairAlexis Rodriguez2025-03-08 11:26:002025-03-08 14:31:00339.64
968repairLaura Chang2025-03-08 17:00:002025-03-08 20:00:00226.18
1039repairMarco Clark2025-03-09 03:00:002025-03-09 07:00:00402.23
maintenance_idnumber · unique id
1
device_idnumber · links to devices
14
maintenance_typetext
calibration
techniciantext
Marco Clark
started_atdate & time
2025-03-04 15:00:00
completed_atdate & time
2025-03-04 15:57:00
costdecimal
97.02
maintenance_idnumber · unique id
2
device_idnumber · links to devices
102
maintenance_typetext
calibration
techniciantext
Mohammed Okafor
started_atdate & time
2025-03-05 16:00:00
completed_atdate & time
2025-03-05 16:29:00
costdecimal
97.13
maintenance_idnumber · unique id
3
device_idnumber · links to devices
172
maintenance_typetext
calibration
techniciantext
Andre Hayes
started_atdate & time
2025-03-06 16:00:00
completed_atdate & time
2025-03-06 17:25:00
costdecimal
67.12
maintenance_idnumber · unique id
4
device_idnumber · links to devices
110
maintenance_typetext
repair
techniciantext
Paul Turner
started_atdate & time
2025-03-07 08:00:00
completed_atdate & time
2025-03-07 10:00:00
costdecimal
425.28
maintenance_idnumber · unique id
5
device_idnumber · links to devices
146
maintenance_typetext
calibration
techniciantext
Mohammed Okafor
started_atdate & time
2025-03-07 19:00:00
completed_atdate & time
2025-03-07 19:24:00
costdecimal
49.06
maintenance_idnumber · unique id
6
device_idnumber · links to devices
31
maintenance_typetext
repair
techniciantext
Daniela Torres
started_atdate & time
2025-03-07 22:00:00
completed_atdate & time
2025-03-08 00:00:00
costdecimal
542.32
maintenance_idnumber · unique id
7
device_idnumber · links to devices
132
maintenance_typetext
repair
techniciantext
Daniela Torres
started_atdate & time
2025-03-08 11:00:00
completed_atdate & time
2025-03-08 14:00:00
costdecimal
378.39
maintenance_idnumber · unique id
8
device_idnumber · links to devices
132
maintenance_typetext
repair
techniciantext
Alexis Rodriguez
started_atdate & time
2025-03-08 11:26:00
completed_atdate & time
2025-03-08 14:31:00
costdecimal
339.64
maintenance_idnumber · unique id
9
device_idnumber · links to devices
68
maintenance_typetext
repair
techniciantext
Laura Chang
started_atdate & time
2025-03-08 17:00:00
completed_atdate & time
2025-03-08 20:00:00
costdecimal
226.18
maintenance_idnumber · unique id
10
device_idnumber · links to devices
39
maintenance_typetext
repair
techniciantext
Marco Clark
started_atdate & time
2025-03-09 03:00:00
completed_atdate & time
2025-03-09 07:00:00
costdecimal
402.23

first 10 of 168 rows · your queries use all of them

solution.sql
 
Output

Hints and the answer are always available. Using them counts the question as solved with help.

Merged maintenance downtime — SQL practice · Runtime