@@ -79,7 +79,6 @@ WHERE crrt_mode IS NOT NULL
7979 SELECT
8080 stay_id, charttime
8181 FROM crrt_stg
82-
8382)
8483SELECT
8584 ie .subject_id
@@ -101,6 +100,33 @@ SELECT
101100 COALESCE(uo .aki_stage_uo ,0 ),
102101 COALESCE(crrt .aki_stage_crrt ,0 )
103102 ) AS aki_stage
103+
104+ -- We intend to combine together the scores from creatinine/UO by left joining
105+ -- from the above temporary table which has all possible charttime.
106+ -- This will guarantee we include all creatinine/UO measurements.
107+
108+ -- However, we have times where urine output is measured, but not creatinine.
109+ -- Thus we end up with NULLs for the creatinine column(s). Naively calculating
110+ -- the highest stage across the columns will often only consider one stage.
111+ -- For example, consider the following rows:
112+ -- stay_id=123, time=10:00, cr_low_7day=4.0, uo_rt_6hr=NULL will give stage 3
113+ -- stay_id=123, time=10:30, cr_low_7day=NULL, uo_rt_6hr=0.3 will give stage 1
114+ -- This results in the stage alternating from low/high across rows.
115+
116+ -- To overcome this, we create a new column which carries forward the highest
117+ -- KDIGO stage from the last 6 hours. In most cases, this smooths out any discontinuity.
118+ , MAX (
119+ GREATEST(
120+ COALESCE(cr .aki_stage_creat ,0 ),
121+ COALESCE(uo .aki_stage_uo ,0 ),
122+ COALESCE(crrt .aki_stage_crrt ,0 )
123+ )
124+ ) OVER
125+ (
126+ PARTITION BY ie .subject_id
127+ ORDER BY DATETIME_DIFF(tm .charttime , ie .intime , SECOND)
128+ RANGE BETWEEN 21600 PRECEDING AND CURRENT ROW
129+ ) AS aki_stage_smoothed
104130FROM ` physionet-data.mimiciv_icu.icustays` ie
105131-- get all possible charttimes as listed in tm_stg
106132LEFT JOIN tm_stg tm
0 commit comments