Skip to content

state_at()

Determine the state at a given time

Since 1.15.0

Determine the state at a given time from a state aggregate. Returns NULL for a time before the first reading. For a time at or after the last reading, returns the last state.

Create a state aggregate and determine the state at a particular time.

SELECT state_at(
(SELECT state_agg(ts, state) FROM states_test),
'2020-01-01 00:00:05+00'
);

Returns:

state_at
----------
START

The syntax is:

state_at(
agg StateAgg,
point TIMESTAMPTZ
) RETURNS TEXT
state_at_int(
agg StateAgg,
point TIMESTAMPTZ
) RETURNS BIGINT
NameTypeDefaultRequiredDescription
aggStateAgg-✔A state aggregate created with state_agg
pointTIMESTAMPTZ-✔The time to get the state at
ColumnTypeDescription
state_at | state_at_intTEXT | BIGINTThe state at the given time.