runtime
118 / 139

task Gaps & islands · hard

Continuous medication coverage

For patients 1–200, merge each patient’s fills of the same medication_name into continuous coverage periods: a fill continues the period when its start_date is no later than the day after the latest stop_date so far. Return patient_id, medication_name, covered_from, covered_to and fills.

what to return

Columns
patient_idmedication_namecovered_fromcovered_tofills in this order
Rows
757 rows · any order
Skill
Build continuous coverage periods from overlapping fills. Refresher: Sessionize an event stream →

expected result

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

expected result757 rows · any order
patient_idmedication_namecovered_fromcovered_tofills
1Cetirizine 10 mg tablet2022-08-072022-09-061
1Metformin 500 mg tablet2022-10-122023-09-212
1Metformin 500 mg tablet2023-09-232024-03-211

first 3 of 757 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.

medications8 columns · 19,275 rows
medication_idnumber · unique idpatient_idnumber · links to patientsencounter_idnumber · links to encountersmedication_nametextstart_datedatestop_datedatedays_supplynumbercostdecimal
111Cetirizine 10 mg tablet2022-08-072022-09-063018.83
212Metformin 500 mg tablet2022-10-122023-04-1018013.33
312Metformin 500 mg tablet2023-03-252023-09-2118014.93
412Metformin 500 mg tablet2023-09-232024-03-2118014.36
529Albuterol 90 mcg inhaler2022-09-032023-03-0218071.18
629Albuterol 90 mcg inhaler2023-03-052023-09-0118070.02
medication_idnumber · unique id
1
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
1
medication_nametext
Cetirizine 10 mg tablet
start_datedate
2022-08-07
stop_datedate
2022-09-06
days_supplynumber
30
costdecimal
18.83
medication_idnumber · unique id
2
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
2
medication_nametext
Metformin 500 mg tablet
start_datedate
2022-10-12
stop_datedate
2023-04-10
days_supplynumber
180
costdecimal
13.33
medication_idnumber · unique id
3
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
2
medication_nametext
Metformin 500 mg tablet
start_datedate
2023-03-25
stop_datedate
2023-09-21
days_supplynumber
180
costdecimal
14.93
medication_idnumber · unique id
4
patient_idnumber · links to patients
1
encounter_idnumber · links to encounters
2
medication_nametext
Metformin 500 mg tablet
start_datedate
2023-09-23
stop_datedate
2024-03-21
days_supplynumber
180
costdecimal
14.36
medication_idnumber · unique id
5
patient_idnumber · links to patients
2
encounter_idnumber · links to encounters
9
medication_nametext
Albuterol 90 mcg inhaler
start_datedate
2022-09-03
stop_datedate
2023-03-02
days_supplynumber
180
costdecimal
71.18
medication_idnumber · unique id
6
patient_idnumber · links to patients
2
encounter_idnumber · links to encounters
9
medication_nametext
Albuterol 90 mcg inhaler
start_datedate
2023-03-05
stop_datedate
2023-09-01
days_supplynumber
180
costdecimal
70.02

first 6 of 19,275 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.

Continuous medication coverage — SQL practice · Runtime