first_val, last_val

This pair of functions returns the values of the first and last points in a CounterSummary aggregate.

first_val(
cs CounterSummary
) RETURNS DOUBLE PRECISION
last_val(
cs CounterSummary
) RETURNS DOUBLE PRECISION

Required arguments

NameTypeDescription
csCounterSummaryThe input CounterSummary from a previous counter_agg (point form) 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 CounterSummary 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,
counter_agg(ts, val) AS cs -- get a CounterSummary
FROM table
GROUP BY time_bucket('1 day'::interval, ts)
)
SELECT
dt,
first_val(cs) -- extract the value of the first point in the CounterSummary
last_val(cs) -- extract the value of the last point in the CounterSummary
FROM t;

Found an issue on this page?

Report an issue!

Related Content