-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-3_mechventflags.sql
71 lines (56 loc) · 1.81 KB
/
2-3_mechventflags.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
-- a column to indicated whether patient received mechvent
-- for each day (1-7)
-- then join into table 9
set search_path to mimiciii;
drop table if exists public.kentran_2_3_mechventflags;
WITH
-- temp table with day 1-7
temp (day) AS (VALUES (1),(2),(3),(4),(5),(6),(7))
, temp_mechvent as
(
select *
from temp cross join public.kentran_2_2_database_ventiles
order by icustay_id, day
)
--select * from temp_mechvent
, tab3 as
(
select
day
, icustay_id
, admittime
, starttime
, endtime
, max(ventnum) over (partition by icustay_id) as mechvent
, case -- flag if a mech event took place during the day
-- start < day-1 * 24 & <= day *24
-- start of event within the day -> Y
when extract(epoch from starttime-admittime)/60/60 > ((day -1) *24)
and extract(epoch from starttime-admittime)/60/60 <= (day *24)
then 'Y'
-- start <= day-1 *24 & end > day * 24
-- start the day before, but end in the future -> Y
when extract(epoch from starttime-admittime)/60/60 <= ((day -1) *24)
and extract(epoch from endtime-admittime)/60/60 > (day *24)
then 'Y'
-- start <= day-1 *24 & end <= day *24 & end > day-1 *24
-- start the day before, but end within the day -> Y
when extract(epoch from starttime-admittime)/60/60 <= ((day -1) *24)
and extract(epoch from endtime-admittime)/60/60 <= (day *24)
and extract(epoch from endtime-admittime)/60/60 > ((day -1) *24)
then 'Y'
else 'N'
end as mechvent_flag
from temp_mechvent
order by icustay_id, day
)
select day
, icustay_id
, mechvent_flag
, count(mechvent_flag) over (partition by icustay_id, day) as mechvent_perday
into public.kentran_2_3_mechventflags
from tab3
where mechvent_flag = 'Y';
-- must include distinct because it was a short cut
select distinct * from public.kentran_2_3_mechventflags
order by icustay_id, day;