1+ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2+ DROP TABLE IF EXISTS vasoactive_agent; CREATE TABLE vasoactive_agent AS
3+ -- This query creates a single table with ongoing doses of vasoactive agents.
4+ -- TBD: rarely angiotensin II, methylene blue, and isoprenaline/isoproterenol are used.
5+ -- these are not in the query currently (they don't appear to be documented in MetaVision).
6+
7+ -- collect all vasopressor administration times
8+ -- create a single table with these as start/stop times
9+ WITH tm AS
10+ (
11+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .dobutamine
12+ UNION DISTINCT
13+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .dopamine
14+ UNION DISTINCT
15+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .epinephrine
16+ UNION DISTINCT
17+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .norepinephrine
18+ UNION DISTINCT
19+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .phenylephrine
20+ UNION DISTINCT
21+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .vasopressin
22+ UNION DISTINCT
23+ SELECT stay_id, starttime AS vasotime FROM mimiciv_derived .milrinone
24+ UNION DISTINCT
25+ -- combine end times from the same tables
26+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .dobutamine
27+ UNION DISTINCT
28+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .dopamine
29+ UNION DISTINCT
30+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .epinephrine
31+ UNION DISTINCT
32+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .norepinephrine
33+ UNION DISTINCT
34+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .phenylephrine
35+ UNION DISTINCT
36+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .vasopressin
37+ UNION DISTINCT
38+ SELECT stay_id, endtime AS vasotime FROM mimiciv_derived .milrinone
39+ )
40+ -- create starttime/endtime from all possible times collected
41+ , tm_lag AS
42+ (
43+ SELECT stay_id
44+ , vasotime AS starttime
45+ -- note: the last row for each partition (stay_id) will have a NULL endtime
46+ -- we can drop this row later, as we know that no vasopressor will start at this time
47+ -- (otherwise, we would have a later end time, which would mean it's not the last row!)
48+ -- QED? :)
49+ , LEAD(vasotime, 1 ) OVER (PARTITION BY stay_id ORDER BY vasotime) AS endtime
50+ FROM tm
51+ )
52+ -- left join to raw data tables to combine doses
53+ SELECT t .stay_id , t .starttime , t .endtime
54+ -- inopressors/vasopressors
55+ , dop .vaso_rate AS dopamine
56+ , epi .vaso_rate AS epinephrine
57+ , nor .vaso_rate AS norepinephrine
58+ , phe .vaso_rate AS phenylephrine
59+ , vas .vaso_rate AS vasopressin
60+ -- inodialators
61+ , dob .vaso_rate AS dobutamine
62+ , mil .vaso_rate AS milrinone
63+ -- isoproterenol is used in CCU/CVICU but not in metavision
64+ -- other drugs not included here but (rarely) used in the BIDMC:
65+ -- angiotensin II, methylene blue
66+ FROM tm_lag t
67+ LEFT JOIN mimiciv_derived .dobutamine dob
68+ ON t .stay_id = dob .stay_id
69+ AND t .starttime >= dob .starttime
70+ AND t .endtime <= dob .endtime
71+ LEFT JOIN mimiciv_derived .dopamine dop
72+ ON t .stay_id = dop .stay_id
73+ AND t .starttime >= dop .starttime
74+ AND t .endtime <= dop .endtime
75+ LEFT JOIN mimiciv_derived .epinephrine epi
76+ ON t .stay_id = epi .stay_id
77+ AND t .starttime >= epi .starttime
78+ AND t .endtime <= epi .endtime
79+ LEFT JOIN mimiciv_derived .norepinephrine nor
80+ ON t .stay_id = nor .stay_id
81+ AND t .starttime >= nor .starttime
82+ AND t .endtime <= nor .endtime
83+ LEFT JOIN mimiciv_derived .phenylephrine phe
84+ ON t .stay_id = phe .stay_id
85+ AND t .starttime >= phe .starttime
86+ AND t .endtime <= phe .endtime
87+ LEFT JOIN mimiciv_derived .vasopressin vas
88+ ON t .stay_id = vas .stay_id
89+ AND t .starttime >= vas .starttime
90+ AND t .endtime <= vas .endtime
91+ LEFT JOIN mimiciv_derived .milrinone mil
92+ ON t .stay_id = mil .stay_id
93+ AND t .starttime >= mil .starttime
94+ AND t .endtime <= mil .endtime
95+ -- remove the final row for each stay_id
96+ -- it will not have any infusions associated with it
97+ WHERE t .endtime IS NOT NULL ;
0 commit comments