Skip to content

Commit a89d9d4

Browse files
committed
propagate linting changes to psql concepts
1 parent 76289ce commit a89d9d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+5754
-4992
lines changed

mimic-iv/concepts_postgres/comorbidity/charlson.sql

+217-134
Large diffs are not rendered by default.

mimic-iv/concepts_postgres/demographics/age.sql

+13-9
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ DROP TABLE IF EXISTS age; CREATE TABLE age AS
1717
-- an admission in 2155 will occur in 2010-2012, and so on.
1818

1919
-- Therefore, the age of a patient = hospital admission time - anchor_year + anchor_age
20-
SELECT
21-
ad.subject_id
22-
, ad.hadm_id
23-
, ad.admittime
24-
, pa.anchor_age
25-
, pa.anchor_year
26-
, DATETIME_DIFF(ad.admittime, DATETIME(pa.anchor_year, 1, 1, 0, 0, 0), 'YEAR') + pa.anchor_age AS age
20+
SELECT
21+
ad.subject_id
22+
, ad.hadm_id
23+
, ad.admittime
24+
, pa.anchor_age
25+
, pa.anchor_year
26+
-- calculate the age as anchor_age (60) plus difference between
27+
-- admit year and the anchor year.
28+
-- the noqa retains the extra long line so the
29+
-- convert to postgres bash script works
30+
, pa.anchor_age + DATETIME_DIFF(ad.admittime, DATETIME(pa.anchor_year, 1, 1, 0, 0, 0), 'YEAR') AS age -- noqa: L016
2731
FROM mimiciv_hosp.admissions ad
2832
INNER JOIN mimiciv_hosp.patients pa
29-
ON ad.subject_id = pa.subject_id
30-
;
33+
ON ad.subject_id = pa.subject_id
34+
;

mimic-iv/concepts_postgres/demographics/icustay_detail.sql

+36-20
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,45 @@
22
DROP TABLE IF EXISTS icustay_detail; CREATE TABLE icustay_detail AS
33
SELECT ie.subject_id, ie.hadm_id, ie.stay_id
44

5-
-- patient level factors
6-
, pat.gender, pat.dod
5+
-- patient level factors
6+
, pat.gender, pat.dod
77

8-
-- hospital level factors
9-
, adm.admittime, adm.dischtime
10-
, DATETIME_DIFF(adm.dischtime, adm.admittime, 'DAY') as los_hospital
11-
, DATETIME_DIFF(adm.admittime, DATETIME(pat.anchor_year, 1, 1, 0, 0, 0), 'YEAR') + pat.anchor_age as admission_age
12-
, adm.race
13-
, adm.hospital_expire_flag
14-
, DENSE_RANK() OVER (PARTITION BY adm.subject_id ORDER BY adm.admittime) AS hospstay_seq
15-
, CASE
16-
WHEN DENSE_RANK() OVER (PARTITION BY adm.subject_id ORDER BY adm.admittime) = 1 THEN True
17-
ELSE False END AS first_hosp_stay
8+
-- hospital level factors
9+
, adm.admittime, adm.dischtime
10+
, DATETIME_DIFF(adm.dischtime, adm.admittime, 'DAY') AS los_hospital
11+
-- calculate the age as anchor_age (60) plus difference between
12+
-- admit year and the anchor year.
13+
-- the noqa retains the extra long line so the
14+
-- convert to postgres bash script works
15+
, pat.anchor_age + DATETIME_DIFF(adm.admittime, DATETIME(pat.anchor_year, 1, 1, 0, 0, 0), 'YEAR') AS admission_age -- noqa: L016
16+
, adm.race
17+
, adm.hospital_expire_flag
18+
, DENSE_RANK() OVER (
19+
PARTITION BY adm.subject_id ORDER BY adm.admittime
20+
) AS hospstay_seq
21+
, CASE
22+
WHEN
23+
DENSE_RANK() OVER (
24+
PARTITION BY adm.subject_id ORDER BY adm.admittime
25+
) = 1 THEN True
26+
ELSE False END AS first_hosp_stay
1827

19-
-- icu level factors
20-
, ie.intime as icu_intime, ie.outtime as icu_outtime
21-
, ROUND(CAST(DATETIME_DIFF(ie.outtime, ie.intime, 'HOUR')/24.0 AS NUMERIC), 2) as los_icu
22-
, DENSE_RANK() OVER (PARTITION BY ie.hadm_id ORDER BY ie.intime) AS icustay_seq
28+
-- icu level factors
29+
, ie.intime AS icu_intime, ie.outtime AS icu_outtime
30+
, ROUND(
31+
CAST(DATETIME_DIFF(ie.outtime, ie.intime, 'HOUR') / 24.0 AS NUMERIC), 2
32+
) AS los_icu
33+
, DENSE_RANK() OVER (
34+
PARTITION BY ie.hadm_id ORDER BY ie.intime
35+
) AS icustay_seq
2336

24-
-- first ICU stay *for the current hospitalization*
25-
, CASE
26-
WHEN DENSE_RANK() OVER (PARTITION BY ie.hadm_id ORDER BY ie.intime) = 1 THEN True
27-
ELSE False END AS first_icu_stay
37+
-- first ICU stay *for the current hospitalization*
38+
, CASE
39+
WHEN
40+
DENSE_RANK() OVER (
41+
PARTITION BY ie.hadm_id ORDER BY ie.intime
42+
) = 1 THEN True
43+
ELSE False END AS first_icu_stay
2844

2945
FROM mimiciv_icu.icustays ie
3046
INNER JOIN mimiciv_hosp.admissions adm

mimic-iv/concepts_postgres/demographics/icustay_hourly.sql

+21-22
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,29 @@ DROP TABLE IF EXISTS icustay_hourly; CREATE TABLE icustay_hourly AS
99
-- this table can be to other tables on ICUSTAY_ID and (ENDTIME - 1 hour,ENDTIME]
1010

1111
-- get first/last measurement time
12-
with all_hours as
13-
(
14-
select
15-
it.stay_id
12+
WITH all_hours AS (
13+
SELECT
14+
it.stay_id
1615

17-
-- ceiling the intime to the nearest hour by adding 59 minutes then truncating
18-
-- note thart we truncate by parsing as string, rather than using DATETIME_TRUNC
19-
-- this is done to enable compatibility with psql
20-
, PARSE_DATETIME(
21-
'%Y-%m-%d %H:00:00',
22-
FORMAT_DATETIME(
23-
'%Y-%m-%d %H:00:00',
24-
DATETIME_ADD(it.intime_hr, INTERVAL '59' MINUTE)
25-
)) AS endtime
16+
-- ceiling the intime to the nearest hour by adding 59 minutes then truncating
17+
-- note thart we truncate by parsing as string, rather than using DATETIME_TRUNC
18+
-- this is done to enable compatibility with psql
19+
, PARSE_DATETIME(
20+
'%Y-%m-%d %H:00:00'
21+
, FORMAT_DATETIME(
22+
'%Y-%m-%d %H:00:00'
23+
, DATETIME_ADD(it.intime_hr, INTERVAL '59' MINUTE)
24+
)) AS endtime
2625

27-
-- create integers for each charttime in hours from admission
28-
-- so 0 is admission time, 1 is one hour after admission, etc, up to ICU disch
29-
-- we allow 24 hours before ICU admission (to grab labs before admit)
30-
, ARRAY(SELECT * FROM generate_series(-24, CEIL(DATETIME_DIFF(it.outtime_hr, it.intime_hr, 'HOUR')))) as hrs
31-
32-
from mimiciv_derived.icustay_times it
26+
-- create integers for each charttime in hours from admission
27+
-- so 0 is admission time, 1 is one hour after admission, etc, up to ICU disch
28+
-- we allow 24 hours before ICU admission (to grab labs before admit)
29+
, ARRAY(SELECT * FROM generate_series(-24, CEIL(DATETIME_DIFF(it.outtime_hr, it.intime_hr, 'HOUR')))) AS hrs -- noqa: L016
30+
FROM mimiciv_derived.icustay_times it
3331
)
32+
3433
SELECT stay_id
35-
, CAST(hr AS bigint) as hr
36-
, DATETIME_ADD(endtime, interval '1' hour * CAST(hr AS bigint)) as endtime
34+
, CAST(hr AS bigint) AS hr
35+
, DATETIME_ADD(endtime, interval '1' hour * CAST(hr AS bigint)) AS endtime
3736
FROM all_hours
38-
CROSS JOIN UNNEST(all_hours.hrs) AS hr;
37+
CROSS JOIN UNNEST(all_hours.hrs) AS hr;

mimic-iv/concepts_postgres/demographics/icustay_times.sql

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ DROP TABLE IF EXISTS icustay_times; CREATE TABLE icustay_times AS
33
-- create a table which has fuzzy boundaries on hospital admission
44
-- involves first creating a lag/lead version of disch/admit time
55
-- get first/last heart rate measurement during hospitalization for each stay_id
6-
WITH t1 AS
7-
(
8-
select ce.stay_id
9-
, min(charttime) as intime_hr
10-
, max(charttime) as outtime_hr
6+
WITH t1 AS (
7+
SELECT ce.stay_id
8+
, MIN(charttime) AS intime_hr
9+
, MAX(charttime) AS outtime_hr
1110
FROM mimiciv_icu.chartevents ce
1211
-- only look at heart rate
13-
where ce.itemid = 220045
14-
group by ce.stay_id
12+
WHERE ce.itemid = 220045
13+
GROUP BY ce.stay_id
1514
)
15+
1616
-- add in subject_id/hadm_id
17-
select
18-
ie.subject_id, ie.hadm_id, ie.stay_id
19-
, t1.intime_hr
20-
, t1.outtime_hr
17+
SELECT
18+
ie.subject_id, ie.hadm_id, ie.stay_id
19+
, t1.intime_hr
20+
, t1.outtime_hr
2121
FROM mimiciv_icu.icustays ie
22-
left join t1
23-
on ie.stay_id = t1.stay_id;
22+
LEFT JOIN t1
23+
ON ie.stay_id = t1.stay_id;

0 commit comments

Comments
 (0)