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