Main Tables Materialized Views Indexes Constraints DDL scrips
Description Columns Query Constraints Triggers

MIMIC2V26.ICUSTAY_DAYS

ContainerUpdatable
ICUSTAY_DAYS N
DDL script

Columns

NameTypeNullableInsertableUpdatableDeletableComment
ICUSTAY_IDNUMBER(7)YYESYESYESUnique icustay identifier
SUBJECT_IDNUMBER(7)YYESYESYESUnique subject identifier
SEQNUMBER(7)YYESYESYESDay number of the ICU stay
BEGINTIMETIMESTAMP(6) WITH TIME ZONEYNONONOStart time of the ICU stay day
ENDTIMETIMESTAMP(6) WITH TIME ZONEYNONONOEnd time of the ICU stay day.
FIRST_DAY_FLGCHAR(1)YYESYESYESThe first day of the ICU stay
LAST_DAY_FLGCHAR(1)YYESYESYESThe last day of the ICU stay

Query:

Note: Syntax highlighting off
WITH Days AS (SELECT
  subject_id,
  icustay_id,
  icustay_day seq,
  intime begintime,
  outtime endtime
FROM mimic2v26.icustayevents
  MODEL RETURN UPDATED ROWS
  PARTITION BY (subject_id,icustay_id)
  DIMENSION BY (0 icustay_day)
  MEASURES (
    intime,
    outtime
    )
  RULES ITERATE (1000)
    UNTIL (ITERATION_NUMBER >= EXTRACT (DAY FROM outtime[0] - intime[0]))
    (
      intime[ITERATION_NUMBER + 1] = intime[0] + NUMTODSINTERVAL(ITERATION_NUMBER, 'DAY'),
      -- Make sure we stay within the time bounds of the ICU stay
      outtime[ITERATION_NUMBER + 1] =
            case
             when (intime[0] + NUMTODSINTERVAL(ITERATION_NUMBER + 1, 'DAY') > outtime[0])
               then outtime[0]
             else intime[0] + NUMTODSINTERVAL(ITERATION_NUMBER + 1, 'DAY') - INTERVAL '1' SECOND
            end
    )
    order by subject_id, icustay_id, intime
)
SELECT
  CAST(icustay_id as number(7,0)) as icustay_id,
  CAST(subject_id as number(7,0)) as subject_id,
  CAST(seq as number(7,0)) as seq,
  begintime,
  endtime,
  CASE
    WHEN ( seq = first_value(seq)
                  OVER (PARTITION BY icustay_id
                            ORDER BY seq) )
         THEN 'Y'
    ELSE 'N'
  END as first_day_flg,
  CASE
    WHEN ( seq = first_value(seq)
                  OVER (PARTITION BY icustay_id
                            ORDER BY seq DESC) )
         THEN 'Y'
    ELSE 'N'
  END as last_day_flg
FROM Days
--WHERE subject_id < 10