runtime
115 / 139

task Correlated subqueries · hard

Device status at the end of each day

For devices 1–20 and every date in March 2025, return device_id, day and status: the status from the latest change on or before the end of that day (NULL before the first change). Ties on changed_at go to the highest status_event_id.

what to return

Columns
device_iddaystatus in this order
Rows
620 rows · any order
Skill
Turn change events into a daily state table. Refresher: Query plans & indexes →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result620 rows · any order
device_iddaystatus
12025-03-01online
12025-03-02online
12025-03-03online

first 3 of 620 rows

data you can query

IoT / Sensors dataset: the first 6 rows of each table. Under each column name: the kind of value it holds, and which table it links to. Your queries run on all rows.

devices9 columns · 186 rows
device_idnumber · unique idserial_numbertextsensor_type_idnumber · links to sensor_typeslocation_idnumber · links to locationsmodeltextfirmware_versiontextreporting_interval_minutesnumberinstalled_atdate & timeretired_attext
1SN-010791911ColdSense 4002.4.2602023-09-13 00:00:00NULL
2SN-011583811ColdSense 4002.4.2602024-06-30 00:00:00NULL
3SN-012375711ThermoTag T23.0.1602024-09-11 00:00:00NULL
4SN-013167611ThermoTag T22.4.2602024-05-31 00:00:00NULL
5SN-033959531EnviroNode E12.4.01202024-09-09 00:00:00NULL
6SN-034751431HygroPro2.4.21202023-07-26 00:00:00NULL
device_idnumber · unique id
1
serial_numbertext
SN-0107919
sensor_type_idnumber · links to sensor_types
1
location_idnumber · links to locations
1
modeltext
ColdSense 400
firmware_versiontext
2.4.2
reporting_interval_minutesnumber
60
installed_atdate & time
2023-09-13 00:00:00
retired_attext
NULL
device_idnumber · unique id
2
serial_numbertext
SN-0115838
sensor_type_idnumber · links to sensor_types
1
location_idnumber · links to locations
1
modeltext
ColdSense 400
firmware_versiontext
2.4.2
reporting_interval_minutesnumber
60
installed_atdate & time
2024-06-30 00:00:00
retired_attext
NULL
device_idnumber · unique id
3
serial_numbertext
SN-0123757
sensor_type_idnumber · links to sensor_types
1
location_idnumber · links to locations
1
modeltext
ThermoTag T2
firmware_versiontext
3.0.1
reporting_interval_minutesnumber
60
installed_atdate & time
2024-09-11 00:00:00
retired_attext
NULL
device_idnumber · unique id
4
serial_numbertext
SN-0131676
sensor_type_idnumber · links to sensor_types
1
location_idnumber · links to locations
1
modeltext
ThermoTag T2
firmware_versiontext
2.4.2
reporting_interval_minutesnumber
60
installed_atdate & time
2024-05-31 00:00:00
retired_attext
NULL
device_idnumber · unique id
5
serial_numbertext
SN-0339595
sensor_type_idnumber · links to sensor_types
3
location_idnumber · links to locations
1
modeltext
EnviroNode E1
firmware_versiontext
2.4.0
reporting_interval_minutesnumber
120
installed_atdate & time
2024-09-09 00:00:00
retired_attext
NULL
device_idnumber · unique id
6
serial_numbertext
SN-0347514
sensor_type_idnumber · links to sensor_types
3
location_idnumber · links to locations
1
modeltext
HygroPro
firmware_versiontext
2.4.2
reporting_interval_minutesnumber
120
installed_atdate & time
2023-07-26 00:00:00
retired_attext
NULL

first 6 of 186 rows · your queries use all of them

device_status_events5 columns · 1,231 rows
status_event_idnumber · unique iddevice_idnumber · links to deviceschanged_atdate & timestatustextreasontext
112025-03-01 00:00:00onlineperiod start
212025-03-07 09:00:00offlinepower loss
312025-03-07 11:00:00onlinereconnected
412025-03-13 09:00:00offlinepower loss
512025-03-13 20:00:00onlinereconnected
612025-04-09 13:00:00degradedreading outside normal range
status_event_idnumber · unique id
1
device_idnumber · links to devices
1
changed_atdate & time
2025-03-01 00:00:00
statustext
online
reasontext
period start
status_event_idnumber · unique id
2
device_idnumber · links to devices
1
changed_atdate & time
2025-03-07 09:00:00
statustext
offline
reasontext
power loss
status_event_idnumber · unique id
3
device_idnumber · links to devices
1
changed_atdate & time
2025-03-07 11:00:00
statustext
online
reasontext
reconnected
status_event_idnumber · unique id
4
device_idnumber · links to devices
1
changed_atdate & time
2025-03-13 09:00:00
statustext
offline
reasontext
power loss
status_event_idnumber · unique id
5
device_idnumber · links to devices
1
changed_atdate & time
2025-03-13 20:00:00
statustext
online
reasontext
reconnected
status_event_idnumber · unique id
6
device_idnumber · links to devices
1
changed_atdate & time
2025-04-09 13:00:00
statustext
degraded
reasontext
reading outside normal range

first 6 of 1,231 rows · your queries use all of them

solution.sql
 

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

Device status at the end of each day — SQL practice · Runtime