interpolated_delta()

Calculate the change in a counter over a time period. Data points at the exact boundaries of the time period aren't needed. The function interpolates the counter values at the boundaries from adjacent CounterSummaries if needed.

interpolated_delta(
summary CounterSummary,
start TIMESTAMPTZ,
interval INTERVAL,
prev CounterSummary,
next CounterSummary
) RETURNS DOUBLE PRECISION

For more information about counter aggregation functions, see the hyperfunctions documentation.

Required arguments

NameTypeDescription
summaryCounterSummaryThe input CounterSummary from a counter_agg call
startTIMESTAMPTZThe start of the interval which the delta should be computed over (if there is a preceeding point)
intervalINTERVALThe length of the interval which the delta should cover

Optional arguments

NameTypeDescription
prevCounterSummaryThe CounterSummary from the prior interval, used to interpolate the value at start. If NULL, the first timestamp in summary is used as the start of the interval.
nextCounterSummaryThe CounterSummary from the following interval, used to interpolate the value at start + interval. If NULL, the last timestamp in summary is used as the end of the interval.

Returns

NameTypeDescription
interpolated_deltaDOUBLE PRECISIONThe delta between the first and last points of the time interval. If exact values are missing in the raw data for the first and last points, these values are interpolated linearly from the neighboring CounterSummaries

Sample usage

SELECT
id,
bucket,
interpolated_delta(
summary,
bucket,
'15 min',
LAG(summary) OVER (PARTITION BY id ORDER by bucket),
LEAD(summary) OVER (PARTITION BY id ORDER by bucket)
)
FROM (
SELECT
id,
time_bucket('15 min'::interval, ts) AS bucket,
counter_agg(ts, val) AS summary
FROM foo
GROUP BY id, time_bucket('15 min'::interval, ts)
) t

Found an issue on this page?

Report an issue!

Related Content