Skip to content

time_weight()

Aggregate data into an intermediate time-weighted aggregate form for further calculation

Since 1.0.0

This is the first step for performing any time-weighted calculations. Use time_weight to create an intermediate aggregate (TimeWeightSummary) from your data. This intermediate form can then be used by one or more accessors in this group to compute final results.

Optionally, multiple such intermediate aggregate objects can be combined using rollup() before an accessor is applied.

Aggregate data from column val into daily time-weighted aggregates, using the linear interpolation method.

SELECT
time_bucket('1 day'::interval, ts) as dt,
time_weight('Linear', ts, val) AS tw
FROM foo
GROUP BY time_bucket('1 day'::interval, ts)

The syntax is:

time_weight(
method TEXT,
ts TIMESTAMPTZ,
value DOUBLE PRECISION
) RETURNS TimeWeightSummary
NameTypeDefaultRequiredDescription
methodTEXT-The weighting method to use. The available methods are linear (or its alias trapezoidal, for those familiar with numeric integration methods) and LOCF, which stands for ‘last observation carried forward’. linear fills in missing data by interpolating linearly between the start and end points of the gap. LOCF fills in the gap by assuming that the value remains constant until the next value is seen. LOCF is most useful when a measurement is taken only when a value changes. linear is most useful if there are no such guarantees on the measurement. The method names are case-insensitive.
tsTIMESTAMPTZ-The time at each point. Null values are ignored. An aggregate evaluated on only null values returns null.
valueDOUBLE PRECISION-The value at each point to use for the time-weighted aggregate. Null values are ignored. An aggregate evaluated on only null values returns null.
ColumnTypeDescription
time_weightTimeWeightSummaryA TimeWeightSummary object that can be passed to other functions within the time-weighting API