ROW_AT_TIME()

All functions > BUSINESS > ROW_AT_TIME()

Returns the row of a history (slowly changing dimension) in effect at the given point in time.

Signatures

Returns: The history's own row type, every field included: the row in effect at point_in_time, or NULL. Read the value with [value]

ROW_AT_TIME(history: ARRAY<T>, point_in_time: TIMESTAMP) → T
sql
ParameterTypeRequiredDescription
historyARRAY<T>YesHistory: one row per state, ordered by time_update, with fields named time_update and value
point_in_timeTIMESTAMPYesPoint in time

With:

  • T : State record: ROW<time_update TIMESTAMP, value V, ...>

Notes

  • A history (slowly changing dimension, SCD type 2) records the states of a value over time
  • Each row has a time_update timestamp and a value; the fields are found by these names (UE/SCD-TYPE-EXPECTED otherwise), and other fields are allowed
  • The result has the history's row type: ROW_AT_TIME(ARRAY<ROW<time_update TIMESTAMP(3), value VARCHAR>>, ts) is a ROW<time_update TIMESTAMP(3), value VARCHAR>, and ROW_AT_TIME(...)[value] is the VARCHAR
  • The history is ordered by creation (time_update ascending, oldest first); the order is trusted, not checked
  • Returns the last row in array order where time_update <= point_in_time: the latest state, and on two equal time_update the later row
  • If no records exist before point_in_time, returns NULL
  • Useful for historical analysis and time travel queries
  • Commonly used for tracking customer attributes, prices, or configurations over time

Availability

StatusBackends
Not availablemysql

Examples

FeatureQL
RETURN
    -- Value at a timestamp between two SCD updates
    f1 := ROW_AT_TIME(
        ARRAY(
            ROW(TIMESTAMP '2022-01-01 00:00:00' AS time_update, 'A' AS value),
            ROW(TIMESTAMP '2022-06-01 00:00:00' AS time_update, 'B' AS value)
        ),
        TIMESTAMP '2022-03-01 00:00:00'
    )[value],
    -- NULL when the point in time is before the first history row
    f2 := ROW_AT_TIME(
        ARRAY(ROW(TIMESTAMP '2022-01-01' AS time_update, 'A' AS value)),
        TIMESTAMP '2021-01-01'
    )[value]
;
Result
f1 VARCHARf2 VARCHAR
ANULL