ROWS_AT_TIME()

All functions > BUSINESS > ROWS_AT_TIME()

Returns the rows of a history alive at the given point in time.

Signatures

Returns: The rows alive at point_in_time, in array order, with the history's row type; a field that is itself a history becomes its value's type

ROWS_AT_TIME(history: ARRAY<T>, point_in_time: TIMESTAMP) → ARRAY<T>
sql
ParameterTypeRequiredDescription
historyARRAY<T>YesHistory: one row per item, ordered by time_create
point_in_timeTIMESTAMPYesPoint in time

With:

  • T : Event record: ROW<time_create: TIMESTAMP, time_update: TIMESTAMP, time_delete: TIMESTAMP, ...>

Notes

  • Each row is an item with a lifecycle: time_create (when it was created) and time_delete (when it was deleted, NULL while it is alive)
  • The history is ordered by creation (time_create ascending); the result keeps array order
  • A row is alive at point_in_time when time_create <= point_in_time < time_delete (or time_delete is NULL): a row is no longer alive at its time_delete
  • A field of a row that is itself a history (ARRAY<ROW<time_update TIMESTAMP, value V>>) is resolved to its value at point_in_time with the ROW_AT_TIME rule: the field's type becomes V
  • Useful for active orders, valid subscriptions, open tickets, event logs
  • Returns an empty array if no row is alive at point_in_time

Availability

StatusBackends
Not availablemysql

Examples

FeatureQL
RETURN
    -- Order ids active at the reference time
    f1 := ROWS_AT_TIME(
        ARRAY(
            ROW(
                10 AS order_id,
                TIMESTAMP '2022-05-05 15:52:06' AS time_create,
                TIMESTAMP '2022-05-05 15:52:06' AS time_update,
                NULL::TIMESTAMP AS time_delete
            )
        ),
        TIMESTAMP '2022-05-06 15:50:00'
    )[order_id],
    -- Empty when the reference time is after time_delete
    f2 := ROWS_AT_TIME(
        ARRAY(
            ROW(
                10 AS order_id,
                TIMESTAMP '2022-05-05' AS time_create,
                TIMESTAMP '2022-05-05' AS time_update,
                TIMESTAMP '2022-05-06' AS time_delete
            )
        ),
        TIMESTAMP '2022-05-07'
    )[order_id],
    -- A row is no longer alive at its time_delete
    f3 := ROWS_AT_TIME(
        ARRAY(
            ROW(
                10 AS order_id,
                TIMESTAMP '2022-05-05' AS time_create,
                TIMESTAMP '2022-05-05' AS time_update,
                TIMESTAMP '2022-05-06' AS time_delete
            )
        ),
        TIMESTAMP '2022-05-06'
    )[order_id]
;
Result
f1 ARRAYf2 ARRAYf3 ARRAY
[10][][]