-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnorepinephrine_info_eicu.sql
277 lines (261 loc) · 10.6 KB
/
norepinephrine_info_eicu.sql
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
drop table if exists `db_name.norepinephrine_info_eicu`;
create table `db_name.norepinephrine_info_eicu` as
-- get all of norepinephrine information with drugrate units of mcg/kg/min
-- 1. add information of unit with 'Norepinephrine ()'
-- process of 'Norepinephrine ()' | 24793
-- we notice that 'drugname = 'Norepinephrine ()'' might happen when :
-- 1) no value -- 0; 2) lose unit for saving time; 3) no-known reasons
-- so : we will solve the 2) by adding units considering the units before and after
with infusiondrug_new_0 as (
select infusiondrugid, patientunitstayid, infusionoffset, drugname
, cast(drugrate as numeric) as drugrate, infusionrate, drugamount, volumeoffluid, patientweight
from `physionet-data.eicu_crd.infusiondrug`
where (
drugname like '%Norepinephrine%'
or drugname like '%norepinephrine%'
)
and drugrate not in (
'Documentation undone'
, 'ERROR'
, 'UD'
, ''
)
and drugrate not like '%OFF%'
)
, norepinephrine_in_part_0 as (
select patientunitstayid
from infusiondrug_new_0
where drugname = 'Norepinephrine ()' -- | 24793
group by patientunitstayid
)
, norepinephrine_in_part_1 as (
select ifd.infusiondrugid
, ifd.patientunitstayid
, ifd.infusionoffset
, ifd.drugname
, case
when ifd.drugname = 'Norepinephrine (mcg/kg/hr)' then 1
when ifd.drugname = 'Norepinephrine (mcg/kg/min)' then 2
when ifd.drugname = 'Norepinephrine (mcg/min)' then 3
when ifd.drugname = 'Norepinephrine (mg/hr)' then 4
when ifd.drugname = 'Norepinephrine (mg/min)' then 5
when ifd.drugname = 'Norepinephrine (ml/hr)' then 6
when ifd.drugname like '%norepinephrine%' then 6
when ifd.drugname = 'Norepinephrine' then 7
else null end as unit_flag -- the imputation function is fit for float value
from infusiondrug_new_0 ifd
inner join norepinephrine_in_part_0 nip
on ifd.patientunitstayid = nip.patientunitstayid
where ifd.drugname like '%Norepinephrine%'
or ifd.drugname like '%norepinephrine%'
)
-- https://stackoverflow.com/questions/41782630/google-big-query-forward-filling-ignore-in-window-function
, norepinephrine_in_part_2 as (
select nip.infusiondrugid
, nip.patientunitstayid
, nip.infusionoffset
, nip.drugname
, nip.unit_flag
, LAST_VALUE(nip.unit_flag IGNORE NULLS) OVER (partition by nip.patientunitstayid order by infusionoffset) as unit_flag_locf
, LAST_VALUE(nip.unit_flag IGNORE NULLS) OVER (partition by nip.patientunitstayid order by infusionoffset desc) as unit_flag_focb
from norepinephrine_in_part_1 nip
)
, norepinephrine_in_part_3 as (
select nip.infusiondrugid
, nip.patientunitstayid
, nip.infusionoffset
, nip.drugname
, nip.unit_flag
, coalesce(nip.unit_flag_locf, nip.unit_flag_focb) as unit_flag_new
from norepinephrine_in_part_2 nip
)
, norepinephrine_in_part_4 as (
select nip.infusiondrugid
, nip.patientunitstayid
, nip.infusionoffset
, nip.drugname
, nip.unit_flag
, case
when nip.unit_flag_new = 1 then 'Norepinephrine (mcg/kg/hr)'
when nip.unit_flag_new = 2 then 'Norepinephrine (mcg/kg/min)'
when nip.unit_flag_new = 3 then 'Norepinephrine (mcg/min)'
when nip.unit_flag_new = 4 then 'Norepinephrine (mg/hr)'
when nip.unit_flag_new = 5 then 'Norepinephrine (mg/min)'
when nip.unit_flag_new = 6 then 'Norepinephrine (ml/hr)'
when nip.unit_flag_new = 7 then 'Norepinephrine'
else null end as drugname_new
from norepinephrine_in_part_3 nip
)
, norepinephrine_in_part_5 as ( -- exist the units of Norepinephrine ()
select nip.infusiondrugid
, nip.patientunitstayid
, nip.infusionoffset
, nip.drugname_new as drugname
, ifd.drugrate
, ifd.infusionrate
, ifd.drugamount
, ifd.volumeoffluid
, ifd.patientweight
from norepinephrine_in_part_4 nip
inner join infusiondrug_new_0 ifd
on nip.infusiondrugid = ifd.infusiondrugid
)
, norepinephrine_in_part_6 as (
select ifd.infusiondrugid
, ifd.patientunitstayid
, ifd.infusionoffset
, ifd.drugname as drugname
, ifd.drugrate
, ifd.infusionrate
, ifd.drugamount
, ifd.volumeoffluid
, ifd.patientweight
from infusiondrug_new_0 ifd
where ifd.drugname like '%Norepinephrine%'
and ifd.patientunitstayid not in (select * from norepinephrine_in_part_0)
)
, norepinephrine_in_part as (
select distinct *
from (
select *
from norepinephrine_in_part_5
union all
select *
from norepinephrine_in_part_6
)
)
-- 2. Unified unit to mcg/kg/min
, norepinephrine_1 as (
select idn.infusiondrugid
, idn.patientunitstayid
, idn.infusionoffset
, idn.drugname
, idn.infusionrate
, idn.drugamount
, idn.volumeoffluid
, idn.patientweight
, case
when idn.drugname in (
'Norepinephrine MAX 32 mg Dextrose 5% 250 ml (mcg/min)' -- | 2585
, 'Norepinephrine STD 4 mg Dextrose 5% 250 ml (mcg/min)' -- | 3013
, 'Norepinephrine STD 32 mg Dextrose 5% 282 ml (mcg/min)' -- | 4
, 'Norepinephrine STD 32 mg Dextrose 5% 500 ml (mcg/min)' -- | 5
, 'Norepinephrine STD 8 mg Dextrose 5% 250 ml (mcg/min)' -- | 8
, 'Norepinephrine STD 4 mg Dextrose 5% 500 ml (mcg/min)' -- | 8
, 'Norepinephrine STD 8 mg Dextrose 5% 500 ml (mcg/min)' -- | 14
, 'Norepinephrine MAX 32 mg Dextrose 5% 500 ml (mcg/min)' -- | 15
)
then drugrate/(coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80)) -- median(admissionweight) = 80
when idn.drugname = 'Norepinephrine (mcg/kg/min)' -- | 68921
then drugrate
when idn.drugname = 'Norepinephrine (mcg/min)' -- | 192059
then drugrate/(coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when idn.drugname = 'Norepinephrine (mcg/hr)' -- | 21
then drugrate/(60 * coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when idn.drugname = 'Norepinephrine (mg/hr)' -- | 57
then 1000*drugrate/(60 * coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when idn.drugname = 'Norepinephrine (mcg/kg/hr)' -- | 58
then drugrate/60
when idn.drugname = 'Norepinephrine (mg/min)' -- | 8
then 1000*drugrate/(coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when idn.drugname = 'Norepinephrine (mg/kg/min)' -- | 9
then 1000*drugrate
else null end as rate_norepinephrine
from norepinephrine_in_part idn
left join `db_name.weight_icustay_detail_modify_eicu` wi
on idn.patientunitstayid = wi.patientunitstayid
where drugname in (
'Norepinephrine MAX 32 mg Dextrose 5% 250 ml (mcg/min)' -- | 2585
, 'Norepinephrine STD 4 mg Dextrose 5% 250 ml (mcg/min)' -- | 3013
, 'Norepinephrine STD 32 mg Dextrose 5% 282 ml (mcg/min)' -- | 4
, 'Norepinephrine STD 32 mg Dextrose 5% 500 ml (mcg/min)' -- | 5
, 'Norepinephrine STD 8 mg Dextrose 5% 250 ml (mcg/min)' -- | 8
, 'Norepinephrine STD 4 mg Dextrose 5% 500 ml (mcg/min)' -- | 8
, 'Norepinephrine STD 8 mg Dextrose 5% 500 ml (mcg/min)' -- | 14
, 'Norepinephrine MAX 32 mg Dextrose 5% 500 ml (mcg/min)' -- | 15
, 'Norepinephrine (mcg/kg/min)' -- | 68921
, 'Norepinephrine (mcg/min)' -- | 192059 -- 192311
, 'Norepinephrine (mcg/hr)' -- | 21
, 'Norepinephrine (mg/hr)' -- | 57
, 'Norepinephrine (mcg/kg/hr)' -- | 58
, 'Norepinephrine (mg/min)' -- | 8 -- 9
, 'Norepinephrine (mg/kg/min)' -- | 9
)
)
-- without consider : Norepinephrine (units/min) | 2; Norepinephrine | 1654 -- 8805
-- norepinephrine Volume (ml) | 95;
, norepinephrine_2 as (
select idn.infusiondrugid
, idn.patientunitstayid
, idn.infusionoffset
, idn.drugname
, idn.infusionrate
, idn.drugamount
, idn.volumeoffluid
, idn.patientweight
, case
when md.drugname in (
'PRMX NOREPInephrine 4 mg/250 mL NS Drip' -- | 1159
, 'NOREPINEPHRINE 4 MG/250 ML NS' -- | 2369
)
or md.drugname like 'NOREPINEPHRINE%4%MG%250%C%REPACKAGE%' -- | 1647
then 1000*idn.drugrate*4/(250*60*coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when md.drugname in (
'250 ML PLAS CONT : NOREPINEPHRINE IN D5W 8 MG/250 ML' -- | 1171
, 'NOREPINEPHRINE 8 MG in 250mL NS' -- | 5569
)
then 1000*idn.drugrate*8/(250*60*coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when md.drugname in (
'NOREPINEPHRINE BITARTRATE 1 MG/ML AMP' -- | 1225
, 'NOREPINEPHRINE BITARTRATE 1 MG/ML IV : 4 ML' -- | 1335
, 'NOREPINEPHRINE BITARTRATE 1 MG/ML IV SOLN' -- | 1919
, 'NOREPINEPHRINE BITARTRATE 1 MG/ML IJ SOLN' -- | 2137
)
then 1000*idn.drugrate/(60*coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80))
when md.drugname = '<<norepinephrine 4 mg/4 mL Inj' -- | 1736
then 0.2 -- clinican thinks this way and ml/hr, patients must be very serious, the part of sofa should be set 4
when md.drugname = 'NOREPINEPHRINE' -- | 2089
and md.dosage = '16 MG'
then 1000*idn.drugrate*16/(250*60*coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80)) -- set mL available : 250ml, maybe not right(just look many ml are 250ml)
when md.drugname = 'NOREPINEPHRINE' -- | 2089
and md.dosage = '4 MG'
then 1000*idn.drugrate*4/(250*60*coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80)) -- set mL available : 250ml, maybe not right
when md.drugname = 'NOREPINEPHRINE' -- | 2089
and md.dosage = '8 MG'
then 1000*idn.drugrate*8/(250*60*coalesce(coalesce(wi.admissionweight, wi.dischargeweight),80)) -- set mL available : 250ml, maybe not right
else null end as rate_norepinephrine
from norepinephrine_in_part idn
inner join `physionet-data.eicu_crd.medication` md
on idn.patientunitstayid = md.patientunitstayid
and md.drugordercancelled = 'No'
and md.drugname like '%Norepinephrine%'
left join `db_name.weight_icustay_detail_modify_eicu` wi
on idn.patientunitstayid = wi.patientunitstayid
where idn.drugname in (
'norepinephrine Volume (ml) (ml/hr)' -- | 13636
, 'Norepinephrine (ml/hr)' -- | 272157 -- 272214
)
and idn.infusionoffset >= md.drugstartoffset
and idn.infusionoffset <= md.drugstopoffset
)
, norepinephrine as (
select distinct *
from (
select *
from norepinephrine_1
union all
select *
from norepinephrine_2
)
)
select infusiondrugid
, patientunitstayid
, infusionoffset
, drugname
, round(rate_norepinephrine,4) as rate_norepinephrine
, infusionrate
, drugamount
, volumeoffluid
, patientweight
from norepinephrine
order by patientunitstayid, infusionoffset;