Skip to content

Commit 70998b9

Browse files
Use second-based DATETIME_DIFF in urine_output_rate
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent c51c46f commit 70998b9

3 files changed

Lines changed: 41 additions & 25 deletions

File tree

‎mimic-iv/concepts/measurement/urine_output_rate.sql‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ WITH tm AS (
1616
)
1717

1818
-- now calculate time since last UO measurement
19+
-- Use SECOND then convert so BigQuery (truncating) and Postgres (fractional)
20+
-- DATETIME_DIFF agree; same approach as kdigo_uo.sql.
1921
, uo_tm AS (
2022
SELECT tm.stay_id
2123
, CASE
2224
WHEN LAG(charttime) OVER w IS NULL
23-
THEN DATETIME_DIFF(charttime, intime_hr, MINUTE)
24-
ELSE DATETIME_DIFF(charttime, LAG(charttime) OVER w, MINUTE)
25+
THEN DATETIME_DIFF(charttime, intime_hr, SECOND) / 60.0
26+
ELSE DATETIME_DIFF(charttime, LAG(charttime) OVER w, SECOND) / 60.0
2527
END AS tm_since_last_uo
2628
, uo.charttime
2729
, uo.urineoutput
@@ -45,26 +47,37 @@ WITH tm AS (
4547
-- to 1 hour of UO, therefore we use '5' and '11' to restrict the
4648
-- period, rather than 6/12 this assumption may overestimate UO rate
4749
-- when documentation is done less than hourly
48-
, SUM(CASE WHEN DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 5
50+
, SUM(CASE
51+
WHEN DATETIME_DIFF(
52+
io.charttime, iosum.charttime, SECOND
53+
) / 3600.0 <= 5
4954
THEN iosum.urineoutput
5055
ELSE NULL END) AS urineoutput_6hr
5156
, ROUND(CAST(
5257
SUM(
5358
CASE
54-
WHEN DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 5
55-
THEN iosum.tm_since_last_uo
56-
ELSE NULL END) / 60.0 AS NUMERIC
59+
WHEN DATETIME_DIFF(
60+
io.charttime, iosum.charttime, SECOND
61+
) / 3600.0 <= 5
62+
THEN iosum.tm_since_last_uo
63+
ELSE NULL END
64+
) / 60.0 AS NUMERIC
5765
), 6) AS uo_tm_6hr
58-
, SUM(CASE WHEN DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 11
66+
, SUM(CASE
67+
WHEN DATETIME_DIFF(
68+
io.charttime, iosum.charttime, SECOND
69+
) / 3600.0 <= 11
5970
THEN iosum.urineoutput
6071
ELSE NULL END) AS urineoutput_12hr
6172
, ROUND(CAST(
6273
SUM(
6374
CASE
64-
WHEN
65-
DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 11
66-
THEN iosum.tm_since_last_uo
67-
ELSE NULL END) / 60.0 AS NUMERIC
75+
WHEN DATETIME_DIFF(
76+
io.charttime, iosum.charttime, SECOND
77+
) / 3600.0 <= 11
78+
THEN iosum.tm_since_last_uo
79+
ELSE NULL END
80+
) / 60.0 AS NUMERIC
6881
), 6) AS uo_tm_12hr
6982
-- 24 hours
7083
, SUM(iosum.urineoutput) AS urineoutput_24hr

‎mimic-iv/concepts_duckdb/measurement/urine_output_rate.sql‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ WITH tm AS (
1818
tm.stay_id,
1919
CASE
2020
WHEN LAG(charttime) OVER w IS NULL
21-
THEN DATE_DIFF('MINUTE', intime_hr, charttime)
22-
ELSE DATE_DIFF('MINUTE', LAG(charttime) OVER w, charttime)
21+
THEN DATE_DIFF('SECOND', intime_hr, charttime) / 60.0
22+
ELSE DATE_DIFF('SECOND', LAG(charttime) OVER w, charttime) / 60.0
2323
END AS tm_since_last_uo,
2424
uo.charttime,
2525
uo.urineoutput
@@ -34,15 +34,15 @@ WITH tm AS (
3434
SUM(DISTINCT io.urineoutput) AS uo,
3535
SUM(
3636
CASE
37-
WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 5
37+
WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 5
3838
THEN iosum.urineoutput
3939
ELSE NULL
4040
END
4141
) AS urineoutput_6hr,
4242
ROUND(
4343
CAST(SUM(
4444
CASE
45-
WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 5
45+
WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 5
4646
THEN iosum.tm_since_last_uo
4747
ELSE NULL
4848
END
@@ -51,15 +51,15 @@ WITH tm AS (
5151
) AS uo_tm_6hr,
5252
SUM(
5353
CASE
54-
WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 11
54+
WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 11
5555
THEN iosum.urineoutput
5656
ELSE NULL
5757
END
5858
) AS urineoutput_12hr,
5959
ROUND(
6060
CAST(SUM(
6161
CASE
62-
WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 11
62+
WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 11
6363
THEN iosum.tm_since_last_uo
6464
ELSE NULL
6565
END

‎mimic-iv/concepts_postgres/measurement/urine_output_rate.sql‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
22
DROP TABLE IF EXISTS mimiciv_derived.urine_output_rate; CREATE TABLE mimiciv_derived.urine_output_rate AS
3-
/* attempt to calculate urine output per hour */ /* rate/hour is the interpretable measure of kidney function */ /* though it is difficult to estimate from aperiodic point measures */ /* first we get the earliest heart rate documented for the stay */
3+
/* attempt to calculate urine output per hour */
4+
/* rate/hour is the interpretable measure of kidney function */
5+
/* though it is difficult to estimate from aperiodic point measures */
6+
/* first we get the earliest heart rate documented for the stay */
47
WITH tm AS (
58
SELECT
69
ie.stay_id,
@@ -14,13 +17,13 @@ WITH tm AS (
1417
AND ce.charttime < ie.outtime + INTERVAL '1' MONTH
1518
GROUP BY
1619
ie.stay_id
17-
), uo_tm /* now calculate time since last UO measurement */ AS (
20+
), uo_tm /* now calculate time since last UO measurement */ /* Use SECOND then convert so BigQuery (truncating) and Postgres (fractional) */ /* DATETIME_DIFF agree; same approach as kdigo_uo.sql. */ AS (
1821
SELECT
1922
tm.stay_id,
2023
CASE
2124
WHEN LAG(charttime) OVER w IS NULL
22-
THEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('minute', charttime) - DATE_TRUNC('minute', intime_hr)) / 60 AS BIGINT)
23-
ELSE CAST(EXTRACT(EPOCH FROM DATE_TRUNC('minute', charttime) - DATE_TRUNC('minute', LAG(charttime) OVER w)) / 60 AS BIGINT)
25+
THEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', charttime) - DATE_TRUNC('second', intime_hr)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 60.0
26+
ELSE CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', charttime) - DATE_TRUNC('second', LAG(charttime) OVER w)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 60.0
2427
END AS tm_since_last_uo,
2528
uo.charttime,
2629
uo.urineoutput
@@ -35,15 +38,15 @@ WITH tm AS (
3538
SUM(DISTINCT io.urineoutput) AS uo, /* note that we assume data charted at charttime corresponds */ /* to 1 hour of UO, therefore we use '5' and '11' to restrict the */ /* period, rather than 6/12 this assumption may overestimate UO rate */ /* when documentation is done less than hourly */
3639
SUM(
3740
CASE
38-
WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 5
41+
WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 5
3942
THEN iosum.urineoutput
4043
ELSE NULL
4144
END
4245
) AS urineoutput_6hr,
4346
ROUND(
4447
CAST(CAST(SUM(
4548
CASE
46-
WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 5
49+
WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 5
4750
THEN iosum.tm_since_last_uo
4851
ELSE NULL
4952
END
@@ -52,15 +55,15 @@ WITH tm AS (
5255
) AS uo_tm_6hr,
5356
SUM(
5457
CASE
55-
WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 11
58+
WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 11
5659
THEN iosum.urineoutput
5760
ELSE NULL
5861
END
5962
) AS urineoutput_12hr,
6063
ROUND(
6164
CAST(CAST(SUM(
6265
CASE
63-
WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 11
66+
WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 11
6467
THEN iosum.tm_since_last_uo
6568
ELSE NULL
6669
END

0 commit comments

Comments
 (0)