|
| 1 | +-- This query extracts: |
| 2 | +-- i) a patient's first code status |
| 3 | +-- ii) a patient's last code status |
| 4 | +-- iii) the time of the first entry of DNR or CMO |
| 5 | + |
| 6 | +with t1 as |
| 7 | +( |
| 8 | + /* |
| 9 | +There are five distinct values for the code status order in the dataset: |
| 10 | +1 DNR / DNI |
| 11 | +2 DNI (do not intubate) |
| 12 | +3 Comfort measures only |
| 13 | +4 Full code |
| 14 | +5 DNR (do not resuscitate) |
| 15 | + */ |
| 16 | + |
| 17 | + select stay_id, charttime, value |
| 18 | + -- use row number to identify first and last code status |
| 19 | + , ROW_NUMBER() over (PARTITION BY stay_id order by charttime) as rnfirst |
| 20 | + , ROW_NUMBER() over (PARTITION BY stay_id order by charttime desc) as rnlast |
| 21 | + -- coalesce the values |
| 22 | + , case |
| 23 | + when value in ('Full code') then 1 |
| 24 | + else 0 end as fullcode |
| 25 | + , case |
| 26 | + when value in ('Comfort measures only') then 1 |
| 27 | + else 0 end as cmo |
| 28 | + , case |
| 29 | + when value in ('DNI (do not intubate)','DNR / DNI') then 1 |
| 30 | + else 0 end as dni |
| 31 | + , case |
| 32 | + when value in ('DNR (do not resuscitate)','DNR / DNI') then 1 |
| 33 | + else 0 end as dnr |
| 34 | + FROM `physionet-data.mimic_icu.chartevents` |
| 35 | + where itemid in (223758) |
| 36 | +) |
| 37 | +select ie.subject_id, ie.hadm_id, ie.stay_id |
| 38 | + -- first recorded code status |
| 39 | + , max(case when rnfirst = 1 then t1.fullcode else null end) as fullcode_first |
| 40 | + , max(case when rnfirst = 1 then t1.cmo else null end) as cmo_first |
| 41 | + , max(case when rnfirst = 1 then t1.dnr else null end) as dnr_first |
| 42 | + , max(case when rnfirst = 1 then t1.dni else null end) as dni_first |
| 43 | + |
| 44 | + -- last recorded code status |
| 45 | + , max(case when rnlast = 1 then t1.fullcode else null end) as fullcode_last |
| 46 | + , max(case when rnlast = 1 then t1.cmo else null end) as cmo_last |
| 47 | + , max(case when rnlast = 1 then t1.dnr else null end) as dnr_last |
| 48 | + , max(case when rnlast = 1 then t1.dni else null end) as dni_last |
| 49 | + |
| 50 | + -- were they *at any time* given a certain code status |
| 51 | + , max(t1.fullcode) as fullcode |
| 52 | + , max(t1.cmo) as cmo |
| 53 | + , max(t1.dnr) as dnr |
| 54 | + , max(t1.dni) as dni |
| 55 | + |
| 56 | + -- time until their first DNR |
| 57 | + , min(case when t1.dnr = 1 then t1.charttime else null end) |
| 58 | + as dnr_first_charttime |
| 59 | + , min(case when t1.dni = 1 then t1.charttime else null end) |
| 60 | + as dni_first_charttime |
| 61 | + |
| 62 | + -- first code status of CMO |
| 63 | + , min(case when t1.cmo = 1 then t1.charttime else null end) |
| 64 | + as timecmo_chart |
| 65 | + |
| 66 | +FROM `physionet-data.mimic_icu.icustays` ie |
| 67 | +left join t1 |
| 68 | + on ie.stay_id = t1.stay_id |
| 69 | +group by ie.subject_id, ie.hadm_id, ie.stay_id, ie.intime; |
0 commit comments