|
| 1 | +-- This query extracts code status with the time at which the |
| 2 | +-- code status was documented. |
| 3 | + |
| 4 | +WITH t1 AS ( |
| 5 | + /* |
| 6 | +There are five distinct values for the code status order in the ICU data: |
| 7 | +1 DNR / DNI |
| 8 | +2 DNI (do not intubate) |
| 9 | +3 Comfort measures only |
| 10 | +4 Full code |
| 11 | +5 DNR (do not resuscitate) |
| 12 | + */ |
| 13 | + |
| 14 | + SELECT |
| 15 | + subject_id |
| 16 | + , hadm_id |
| 17 | + , stay_id |
| 18 | + , charttime |
| 19 | + -- coalesce the values |
| 20 | + , CASE |
| 21 | + WHEN value IN ('Full code') THEN 1 |
| 22 | + ELSE 0 END AS fullcode |
| 23 | + , CASE |
| 24 | + WHEN value IN ('Comfort measures only') THEN 1 |
| 25 | + ELSE 0 END AS cmo |
| 26 | + , CASE |
| 27 | + WHEN value IN ('DNI (do not intubate)', 'DNR / DNI') THEN 1 |
| 28 | + ELSE 0 END AS dni |
| 29 | + , CASE |
| 30 | + WHEN value IN ('DNR (do not resuscitate)', 'DNR / DNI') THEN 1 |
| 31 | + ELSE 0 END AS dnr |
| 32 | + FROM `physionet-data.mimic_icu.chartevents` |
| 33 | + WHERE itemid IN (223758) |
| 34 | +) |
| 35 | + |
| 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 |
| 49 | + |
| 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 | +) |
| 66 | + |
| 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