first_val, last_val

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

  1. first_val(
  2. cs CounterSummary
  3. ) RETURNS DOUBLE PRECISION
  4. last_val(
  5. cs CounterSummary
  6. ) 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:

  1. WITH t as (
  2. SELECT
  3. time_bucket('1 day'::interval, ts) as dt,
  4. counter_agg(ts, val) AS cs -- get a CounterSummary
  5. FROM table
  6. GROUP BY time_bucket('1 day'::interval, ts)
  7. )
  8. SELECT
  9. dt,
  10. first_val(cs) -- extract the value of the first point in the CounterSummary
  11. last_val(cs) -- extract the value of the last point in the CounterSummary
  12. FROM t;