33-- ii) a patient's last code status
44-- iii) the time of the first entry of DNR or CMO
55
6- with t1 as (
6+ WITH t1 AS (
77 /*
88There are five distinct values for the code status order in the dataset:
991 DNR / DNI
@@ -13,69 +13,69 @@ There are five distinct values for the code status order in the dataset:
13135 DNR (do not resuscitate)
1414 */
1515
16- select
17- stay_id,
18- charttime,
19- value,
16+ SELECT
17+ stay_id
18+ , charttime
19+ , value
2020 -- 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,
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
2525 -- 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 )
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 )
4040)
4141
42- select
43- ie .subject_id ,
44- ie .hadm_id ,
45- ie .stay_id ,
42+ SELECT
43+ ie .subject_id
44+ , ie .hadm_id
45+ , ie .stay_id
4646 -- 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,
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
5353
5454 -- 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,
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
6161
6262 -- 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,
63+ , MAX (t1 .fullcode ) AS fullcode
64+ , MAX (t1 .cmo ) AS cmo
65+ , MAX (t1 .dnr ) AS dnr
66+ , MAX (t1 .dni ) AS dni
6767
6868 -- 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,
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
7373
7474 -- first code status of CMO
75- MIN (case when t1 .cmo = 1 then t1 .charttime end )
76- as timecmo_chart
75+ , MIN (CASE WHEN t1 .cmo = 1 THEN t1 .charttime END )
76+ AS timecmo_chart
7777
78- from ` physionet-data.mimic_icu.icustays` as ie
79- left join t1
80- on ie .stay_id = t1 .stay_id
81- group by ie .subject_id , ie .hadm_id , ie .stay_id , ie .intime ;
78+ FROM ` physionet-data.mimic_icu.icustays` AS ie
79+ LEFT JOIN t1
80+ ON ie .stay_id = t1 .stay_id
81+ GROUP BY ie .subject_id , ie .hadm_id , ie .stay_id , ie .intime ;
0 commit comments