Skip to main content
Since 1.0.0 Calculate the time-weighted average. Equal to integral divided by the elapsed time. Note that there is a key difference to avg(): If there is exactly one value, avg() would return that value, but average() returns NULL.

Arguments

NameTypeDefaultRequiredDescription
twsTimeWeightSummary-The input TimeWeightSummary from a time_weight() call

Returns

ColumnTypeDescription
averageDOUBLE PRECISIONThe time-weighted average

Samples

Calculate the time-weighted average of the column val, using the ‘last observation carried forward’ interpolation method:
SELECT
    id,
    average(tws)
FROM (
    SELECT
        id,
        time_weight('LOCF', ts, val) AS tws
    FROM foo
    GROUP BY id
) t