Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use CONCAT with CAST to create interval for DATETIME_ADD #1847

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions mimic-iv/concepts_postgres/demographics/icustay_hourly.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
DROP TABLE IF EXISTS mimiciv_derived.icustay_hourly; CREATE TABLE mimiciv_derived.icustay_hourly AS
/* This query generates a row for every hour the patient is in the ICU. */ /* The hours are based on clock-hours (i.e. 02:00, 03:00). */ /* The hour clock starts 24 hours before the first heart rate measurement. */ /* Note that the time of the first heart rate measurement is ceilinged to */ /* the hour. */ /* this query extracts the cohort and every possible hour they were in the ICU */ /* this table can be to other tables on stay_id and (ENDTIME - 1 hour,ENDTIME] */ /* get first/last measurement time */
DROP TABLE IF EXISTS icustay_hourly; CREATE TABLE icustay_hourly AS
-- This query generates a row for every hour the patient is in the ICU.
-- The hours are based on clock-hours (i.e. 02:00, 03:00).
-- The hour clock starts 24 hours before the first heart rate measurement.
-- Note that the time of the first heart rate measurement is ceilinged to
-- the hour.

-- this query extracts the cohort and every possible hour they were in the ICU
-- this table can be to other tables on stay_id and (ENDTIME - 1 hour,ENDTIME]

-- get first/last measurement time
WITH all_hours AS (
SELECT
it.stay_id, /* round the intime up to the nearest hour */
CASE
WHEN DATE_TRUNC('HOUR', it.intime_hr) = it.intime_hr
THEN it.intime_hr
ELSE DATE_TRUNC('HOUR', it.intime_hr) + INTERVAL '1 HOUR'
END AS endtime, /* create integers for each charttime in hours from admission */ /* so 0 is admission time, 1 is one hour after admission, etc, */ /* up to ICU disch */ /* we allow 24 hours before ICU admission (to grab labs before admit) */
ARRAY(SELECT
*
FROM GENERATE_SERIES(-24, CAST(CEIL(EXTRACT(EPOCH FROM it.outtime_hr - it.intime_hr) / 3600.0) AS INT))) AS hrs /* noqa: L016 */
FROM mimiciv_derived.icustay_times AS it
SELECT
it.stay_id

-- round the intime up to the nearest hour
, CASE
WHEN DATE_TRUNC('HOUR', it.intime_hr) = it.intime_hr
THEN it.intime_hr
ELSE
DATETIME_ADD(
DATE_TRUNC('HOUR', it.intime_hr), INTERVAL '1' HOUR
)
END AS endtime
, hrs
-- -- create integers for each charttime in hours from admission
-- -- so 0 is admission time, 1 is one hour after admission, etc,
-- -- up to ICU disch
-- -- we allow 24 hours before ICU admission (to grab labs before admit)
-- , GENERATE_ARRAY(-24, CAST(CEIL(DATETIME_DIFF(it.outtime_hr, it.intime_hr, 'HOUR')) AS INTEGER)) AS hrs -- noqa: L016
FROM mimiciv_derived.icustay_times it
CROSS JOIN GENERATE_ARRAY ( - 24, CAST ( CEIL ( DATETIME_DIFF ( it.outtime_hr, it.intime_hr, 'HOUR' ) ) AS INTEGER ) ) AS hrs
)
SELECT
stay_id,
CAST(hr_unnested AS BIGINT) AS hr,
endtime + CAST(hr_unnested AS BIGINT) * INTERVAL '1 HOUR' AS endtime
SELECT stay_id
, CAST ( hrs AS INTEGER ) AS hr
, DATETIME_ADD(endtime, concat( CAST ( hrs AS INTEGER ) ,' HOUR')::interval) AS endtime
FROM all_hours
CROSS JOIN UNNEST(all_hours.hrs) AS _t0(hr_unnested)
Loading