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
1+ -- This query extracts code status with the time at which the
2+ -- code status was documented.
53
64WITH t1 AS (
75 /*
8- There are five distinct values for the code status order in the dataset :
6+ There are five distinct values for the code status order in the ICU data :
971 DNR / DNI
1082 DNI (do not intubate)
1193 Comfort measures only
@@ -14,14 +12,10 @@ There are five distinct values for the code status order in the dataset:
1412 */
1513
1614 SELECT
17- stay_id
15+ subject_id
16+ , hadm_id
17+ , stay_id
1818 , 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
2519 -- coalesce the values
2620 , CASE
2721 WHEN value IN (' Full code' ) THEN 1
@@ -39,43 +33,46 @@ There are five distinct values for the code status order in the dataset:
3933 WHERE itemid IN (223758 )
4034)
4135
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
61-
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
67-
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
36+ -- Provider order entry contains hospital wide orders for code status changes
37+ -- Interestingly, it does not contain comfort measures only orders
38+ , poe AS (
39+ SELECT p .subject_id
40+ , p .hadm_id
41+ , ie .stay_id
42+ , p .order_time
43+ , CASE
44+ WHEN pd .field_value = ' Resuscitate (Full code)' THEN 1
45+ WHEN pd .field_value = ' Full code (attempt resuscitation)' THEN 1
46+ ELSE 0 END AS fullcode
47+ , CASE
48+ WHEN
7349
74- -- first code status of CMO
75- , MIN (CASE WHEN t1 .cmo = 1 THEN t1 .charttime END)
76- AS timecmo_chart
50+ pd .field_value = ' DNAR (DO NOT attempt resuscitation for cardiac arrest) ' THEN 1 -- noqa
51+ WHEN pd .field_value = ' Do not resuscitate (DNR/DNI)' THEN 1
52+ ELSE 0 END AS dnr
53+ , CASE
54+ WHEN pd .field_value = ' Do not resuscitate (DNR/DNI)' THEN 1
55+ ELSE 0 END AS dni
56+ FROM ` physionet-data.mimiciv_hosp.poe` p
57+ INNER JOIN ` physionet-data.mimiciv_hosp.poe_detail` pd
58+ ON p .poe_id = pd .poe_id
59+ LEFT JOIN ` physiont-data.mimiciv_icu.icustays` ie
60+ ON p .hadm_id = ie .hadm_id
61+ AND p .order_time >= ie .intime
62+ AND p .order_time <= ie .outtime
63+ WHERE p .order_type = ' General Care'
64+ AND order_subtype = ' Code status'
65+ )
7766
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 ;
67+ -- Merge together code status from ICU data
68+ -- with code status from provider order entry
69+ SELECT t1 .subject_id , t1 .hadm_id , t1 .stay_id
70+ , t1 .charttime
71+ , t1 .fullcode , t1 .cmo , t1 .dni , t1 .dnr
72+ FROM t1
73+ UNION ALL
74+ SELECT poe .subject_id , poe .hadm_id , poe .stay_id
75+ , poe .order_time AS charttime
76+ , poe .fullcode , 0 AS cmo, poe .dni , poe .dnr
77+ FROM poe
78+ ;
0 commit comments