runtime
122 / 139

task Correlated subqueries · medium

Each patient’s latest systolic reading

For patients 1–400 with at least one known systolic reading (code BP_SYS), return patient_id, observed_at and value of the latest one (ties by highest observation_id).

what to return

Columns
patient_idobserved_atvalue in this order
Rows
372 rows · any order
Skill
Pick the most recent measurement per entity. Refresher: Subqueries →

expected result

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

expected result372 rows · any order
patient_idobserved_atvalue
12025-04-13 19:26:00129
22025-05-28 11:20:00125
32024-11-24 12:22:00111

first 3 of 372 rows

data you can query

Healthcare 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.

observations8 columns · 39,529 rows
observation_idnumber · unique idpatient_idnumber · links to patientsencounter_idnumber · links to encountersobserved_atdate & timecodetextdescriptiontextvaluedecimalunittext
1112022-08-07 13:18:00BP_SYSSystolic blood pressure126mmHg
2112022-08-07 13:05:00BP_DIADiastolic blood pressure79mmHg
3122022-10-12 11:50:00A1CHemoglobin A1c5.96%
4132023-05-12 12:02:00A1CHemoglobin A1c6.06%
5142023-12-17 08:40:00BP_SYSSystolic blood pressure122mmHg
6142023-12-17 08:33:00BP_DIADiastolic blood pressure71mmHg
observation_idnumber · unique id
1
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
1
observed_atdate & time
2022-08-07 13:18:00
codetext
BP_SYS
descriptiontext
Systolic blood pressure
valuedecimal
126
unittext
mmHg
observation_idnumber · unique id
2
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
1
observed_atdate & time
2022-08-07 13:05:00
codetext
BP_DIA
descriptiontext
Diastolic blood pressure
valuedecimal
79
unittext
mmHg
observation_idnumber · unique id
3
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
2
observed_atdate & time
2022-10-12 11:50:00
codetext
A1C
descriptiontext
Hemoglobin A1c
valuedecimal
5.96
unittext
%
observation_idnumber · unique id
4
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
3
observed_atdate & time
2023-05-12 12:02:00
codetext
A1C
descriptiontext
Hemoglobin A1c
valuedecimal
6.06
unittext
%
observation_idnumber · unique id
5
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
4
observed_atdate & time
2023-12-17 08:40:00
codetext
BP_SYS
descriptiontext
Systolic blood pressure
valuedecimal
122
unittext
mmHg
observation_idnumber · unique id
6
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
4
observed_atdate & time
2023-12-17 08:33:00
codetext
BP_DIA
descriptiontext
Diastolic blood pressure
valuedecimal
71
unittext
mmHg

first 6 of 39,529 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.

Each patient’s latest systolic reading — SQL practice · Runtime