runtime
44 / 139

task Validation & types · hard

Overlapping prescription fills

For patients 1–300, return patient_id, medication_name, first_id and second_id for every pair of fills of the same drug for the same patient whose supply intervals overlap. Intervals are [start_date, stop_date); list each pair once with first_id < second_id.

what to return

Columns
patient_idmedication_namefirst_idsecond_id in this order
Rows
441 rows · any order
Skill
Detect overlapping intervals within a group. Refresher: Product validity with LEAD & half-open intervals →

expected result

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

expected result441 rows · any order
patient_idmedication_namefirst_idsecond_id
1Metformin 500 mg tablet23
2Albuterol 90 mcg inhaler67
2Albuterol 90 mcg inhaler78

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

Overlapping prescription fills — SQL practice · Runtime