-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathpivoted_bg_art.sql
More file actions
174 lines (162 loc) · 5.21 KB
/
pivoted_bg_art.sql
File metadata and controls
174 lines (162 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
-- =====================================================================
-- PostgreSQL version of BigQuery pivoted-bg-art.sql (MIMIC-III)
-- Requires: mimiciii_derived.pivoted_bg
--
-- Output: mimiciii_derived.pivoted_bg_art
-- =====================================================================
DROP TABLE IF EXISTS mimiciii_derived.pivoted_bg_art;
CREATE TABLE mimiciii_derived.pivoted_bg_art AS
WITH stg_spo2 AS
(
SELECT
hadm_id
, charttime
, AVG(valuenum) AS spo2
FROM mimiciii_clinical.chartevents
WHERE itemid IN (646, 220277) -- SpO2
AND valuenum > 0 AND valuenum <= 100
AND charttime IS NOT NULL
GROUP BY hadm_id, charttime
)
, stg_fio2 AS
(
SELECT
hadm_id
, charttime
, MAX(
CASE
WHEN itemid = 223835 THEN
CASE
WHEN valuenum > 0 AND valuenum <= 1 THEN valuenum * 100
WHEN valuenum > 1 AND valuenum < 21 THEN NULL
WHEN valuenum >= 21 AND valuenum <= 100 THEN valuenum
ELSE NULL
END
WHEN itemid IN (3420, 3422) THEN
valuenum
WHEN itemid = 190 AND valuenum > 0.20 AND valuenum < 1 THEN
valuenum * 100
ELSE NULL
END
) AS fio2_chartevents
FROM mimiciii_clinical.chartevents
WHERE itemid IN (3420, 190, 223835, 3422)
AND valuenum > 0 AND valuenum < 100
AND charttime IS NOT NULL
-- exclude rows marked as error (if column exists in your import)
AND (error IS NULL OR error <> 1)
GROUP BY hadm_id, charttime
)
, stg2 AS
(
SELECT
bg.*
, ROW_NUMBER() OVER
(PARTITION BY bg.hadm_id, bg.charttime
ORDER BY s1.charttime DESC NULLS LAST) AS lastrowspo2
, s1.spo2
FROM mimiciii_derived.pivoted_bg bg
LEFT JOIN stg_spo2 s1
ON bg.hadm_id = s1.hadm_id
AND s1.charttime BETWEEN (bg.charttime - INTERVAL '2' HOUR) AND bg.charttime
WHERE bg.po2 IS NOT NULL
)
, stg3 AS
(
SELECT
bg.*
, ROW_NUMBER() OVER
(PARTITION BY bg.hadm_id, bg.charttime
ORDER BY s2.charttime DESC NULLS LAST) AS lastrowfio2
, s2.fio2_chartevents
-- Logistic regression probability (same coefficients as BigQuery)
, 1.0 / (1.0 + EXP(-(
-0.02544
+ 0.04598 * po2
+ COALESCE(-0.15356 * spo2 , -0.15356 * 97.49420 + 0.13429)
+ COALESCE( 0.00621 * fio2_chartevents, 0.00621 * 51.49550 - 0.24958)
+ COALESCE( 0.10559 * hemoglobin , 0.10559 * 10.32307 + 0.05954)
+ COALESCE( 0.13251 * so2 , 0.13251 * 93.66539 - 0.23172)
+ COALESCE(-0.01511 * pco2 , -0.01511 * 42.08866 - 0.01630)
+ COALESCE( 0.01480 * fio2 , 0.01480 * 63.97836 - 0.31142)
+ COALESCE(-0.00200 * aado2 , -0.00200 * 442.21186 - 0.01328)
+ COALESCE(-0.03220 * bicarbonate , -0.03220 * 22.96894 - 0.06535)
+ COALESCE( 0.05384 * totalco2 , 0.05384 * 24.72632 - 0.01405)
+ COALESCE( 0.08202 * lactate , 0.08202 * 3.06436 + 0.06038)
+ COALESCE( 0.10956 * ph , 0.10956 * 7.36233 - 0.00617)
+ COALESCE( 0.00848 * o2flow , 0.00848 * 7.59362 - 0.35803)
))) AS specimen_prob
FROM stg2 bg
LEFT JOIN stg_fio2 s2
ON bg.hadm_id = s2.hadm_id
AND s2.charttime BETWEEN (bg.charttime - INTERVAL '4' HOUR) AND bg.charttime
AND s2.fio2_chartevents > 0
WHERE bg.lastrowspo2 = 1
)
SELECT
stg3.hadm_id
, stg3.icustay_id
, stg3.charttime
, stg3.specimen
, CASE
WHEN stg3.specimen IS NOT NULL THEN stg3.specimen
WHEN stg3.specimen_prob > 0.75 THEN 'ART'
ELSE NULL
END AS specimen_pred
, stg3.specimen_prob
-- oxygen related parameters
, stg3.so2
, stg3.spo2
, stg3.po2
, stg3.pco2
, stg3.fio2_chartevents
, stg3.fio2
, stg3.aado2
, CASE
WHEN stg3.po2 IS NOT NULL
AND stg3.pco2 IS NOT NULL
AND COALESCE(stg3.fio2, stg3.fio2_chartevents) IS NOT NULL
THEN (COALESCE(stg3.fio2, stg3.fio2_chartevents) / 100.0) * (760 - 47) - (stg3.pco2 / 0.8) - stg3.po2
ELSE NULL
END AS aado2_calc
, CASE
WHEN stg3.po2 IS NOT NULL
AND COALESCE(stg3.fio2, stg3.fio2_chartevents) IS NOT NULL
THEN 100.0 * stg3.po2 / COALESCE(stg3.fio2, stg3.fio2_chartevents)
ELSE NULL
END AS pao2fio2ratio
-- acid-base parameters
, stg3.ph
, stg3.baseexcess
, stg3.bicarbonate
, stg3.totalco2
-- blood count parameters
, stg3.hematocrit
, stg3.hemoglobin
, stg3.carboxyhemoglobin
, stg3.methemoglobin
-- chemistry
, stg3.chloride
, stg3.calcium
, stg3.temperature
, stg3.potassium
, stg3.sodium
, stg3.lactate
, stg3.glucose
-- ventilation / misc
, stg3.intubated
, stg3.tidalvolume
, stg3.ventilationrate
, stg3.ventilator
, stg3.peep
, stg3.o2flow
, stg3.requiredo2
FROM stg3
WHERE stg3.lastrowfio2 = 1
AND (stg3.specimen = 'ART' OR stg3.specimen_prob > 0.75)
ORDER BY stg3.hadm_id, stg3.charttime;
-- Suggested indexes (optional but strongly recommended)
-- CREATE INDEX IF NOT EXISTS idx_pivoted_bg_art_icustay_charttime
-- ON mimiciii_derived.pivoted_bg_art (icustay_id, charttime);
-- CREATE INDEX IF NOT EXISTS idx_pivoted_bg_art_hadm_charttime
-- ON mimiciii_derived.pivoted_bg_art (hadm_id, charttime);