|
| 1 | +-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. |
| 2 | +DROP TABLE IF EXISTS code_status; CREATE TABLE code_status AS |
| 3 | +-- This query extracts: |
| 4 | +-- i) a patient's first code status |
| 5 | +-- ii) a patient's last code status |
| 6 | +-- iii) the time of the first entry of DNR or CMO |
| 7 | + |
| 8 | +with t1 as |
| 9 | +( |
| 10 | + select icustay_id, charttime, value |
| 11 | + -- use row number to identify first and last code status |
| 12 | + , ROW_NUMBER() over (PARTITION BY icustay_id order by charttime) as rnfirst |
| 13 | + , ROW_NUMBER() over (PARTITION BY icustay_id order by charttime desc) as rnlast |
| 14 | + |
| 15 | + -- coalesce the values |
| 16 | + , case |
| 17 | + when value in ('Full Code','Full code') then 1 |
| 18 | + else 0 end as fullcode |
| 19 | + , case |
| 20 | + when value in ('Comfort Measures','Comfort measures only') then 1 |
| 21 | + else 0 end as cmo |
| 22 | + , case |
| 23 | + when value = 'CPR Not Indicate' then 1 |
| 24 | + else 0 end as dncpr -- only in CareVue, i.e. only possible for ~60-70% of patients |
| 25 | + , case |
| 26 | + when value in ('Do Not Intubate','DNI (do not intubate)','DNR / DNI') then 1 |
| 27 | + else 0 end as dni |
| 28 | + , case |
| 29 | + when value in ('Do Not Resuscita','DNR (do not resuscitate)','DNR / DNI') then 1 |
| 30 | + else 0 end as dnr |
| 31 | + FROM chartevents |
| 32 | + where itemid in (128, 223758) |
| 33 | + and value is not null |
| 34 | + and value != 'Other/Remarks' |
| 35 | + -- exclude rows marked as error |
| 36 | + AND (error IS NULL OR error = 0) |
| 37 | +) |
| 38 | +select ie.subject_id, ie.hadm_id, ie.icustay_id |
| 39 | + -- first recorded code status |
| 40 | + , max(case when rnfirst = 1 then t1.fullcode else null end) as fullcode_first |
| 41 | + , max(case when rnfirst = 1 then t1.cmo else null end) as cmo_first |
| 42 | + , max(case when rnfirst = 1 then t1.dnr else null end) as dnr_first |
| 43 | + , max(case when rnfirst = 1 then t1.dni else null end) as dni_first |
| 44 | + , max(case when rnfirst = 1 then t1.dncpr else null end) as dncpr_first |
| 45 | + |
| 46 | + -- last recorded code status |
| 47 | + , max(case when rnlast = 1 then t1.fullcode else null end) as fullcode_last |
| 48 | + , max(case when rnlast = 1 then t1.cmo else null end) as cmo_last |
| 49 | + , max(case when rnlast = 1 then t1.dnr else null end) as dnr_last |
| 50 | + , max(case when rnlast = 1 then t1.dni else null end) as dni_last |
| 51 | + , max(case when rnlast = 1 then t1.dncpr else null end) as DNCPR_last |
| 52 | + |
| 53 | + -- were they *at any time* given a certain code status |
| 54 | + , max(t1.fullcode) as fullcode |
| 55 | + , max(t1.cmo) as cmo |
| 56 | + , max(t1.dnr) as dnr |
| 57 | + , max(t1.dni) as dni |
| 58 | + , max(t1.dncpr) as dncpr |
| 59 | + |
| 60 | + -- time until their first DNR |
| 61 | + , min(case when t1.dnr = 1 then t1.charttime else null end) |
| 62 | + as dnr_first_charttime |
| 63 | + , min(case when t1.dni = 1 then t1.charttime else null end) |
| 64 | + as dni_first_charttime |
| 65 | + , min(case when t1.dncpr = 1 then t1.charttime else null end) |
| 66 | + as dncpr_first_charttime |
| 67 | + |
| 68 | + -- first code status of CMO |
| 69 | + , min(case when t1.cmo = 1 then t1.charttime else null end) |
| 70 | + as timecmo_chart |
| 71 | + |
| 72 | +FROM icustays ie |
| 73 | +left join t1 |
| 74 | + on ie.icustay_id = t1.icustay_id |
| 75 | +group by ie.subject_id, ie.hadm_id, ie.icustay_id, ie.intime; |
0 commit comments