first_val, last_val

This pair of functions returns the timestamps of the first and last points in a TimeWeightSummary aggregate.

first_val(
tw TimeWeightSummary
) RETURNS DOUBLE PRECISION
last_val(
tw TimeWeightSummary
) RETURNS DOUBLE PRECISION

Required arguments

NameTypeDescription
twTimeWeightSummaryThe input TimeWeightSummary from a previous time_weight call, often from a continuous aggregate

Returns

ColumnTypeDescription
first_valDOUBLE PRECISIONThe value of the first point in the CounterSummary
ColumnTypeDescription
last_valDOUBLE PRECISIONThe value of the last point in the CounterSummary

Sample usage

This example produces a linear TimeWeightSummary from timestamps and associated values, then applies the first_val and last_val accessors:

WITH t as (
SELECT
time_bucket('1 day'::interval, ts) as dt,
time_weight('Linear', ts, val) AS tw -- get a TimeWeightSummary
FROM table
GROUP BY time_bucket('1 day'::interval, ts)
)
SELECT
dt,
first_val(tw) -- extract the value of the first point in the TimeWeightSummary
last_val(tw) -- extract the value of the last point in the TimeWeightSummary
FROM t;

Found an issue on this page?

Report an issue!

Related Content