Skip to main content
Since 1.3.0 Perform common statistical analyses, such as calculating averages and standard deviations, using this group of functions. These functions are similar to the PostgreSQL statistical aggregates, but they include more features and are easier to use in continuous aggregates and window functions. These functions work on one-dimensional data. To work with two-dimensional data, for example to perform linear regression, see the two-dimensional stats_agg functions.

Two-step aggregation

This group of functions uses the two-step aggregation pattern. Rather than calculating the final result in one step, you first create an intermediate aggregate by using the aggregate function. Then, use any of the accessors on the intermediate aggregate to calculate a final result. You can also roll up multiple intermediate aggregates with the rollup functions. The two-step aggregation pattern has several advantages:
  1. More efficient because multiple accessors can reuse the same aggregate
  2. Easier to reason about performance, because aggregation is separate from final computation
  3. Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
  4. Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result
To learn more, see the blog post on two-step aggregates.

Samples

Calculate statistical properties

Create a statistical aggregate to summarize daily statistical data about the variable val1. Use the statistical aggregate to calculate average, standard deviation, and skewness of the variable:
WITH t AS (
    SELECT
        time_bucket('1 day'::interval, ts) AS dt,
        stats_agg(val1) AS stats1D
    FROM foo
    WHERE id = 'bar'
    GROUP BY time_bucket('1 day'::interval, ts)
)
SELECT
    average(stats1D),
    stddev(stats1D),
    skewness(stats1D)
FROM t;

Available functions

Aggregate

  • stats_agg(): aggregate data into an intermediate statistical aggregate form for further calculation

Accessors

  • average(): calculate the average from a statistical aggregate
  • stddev(): calculate the standard deviation from a statistical aggregate
  • variance(): calculate the variance from a statistical aggregate
  • skewness(): calculate the skewness from a statistical aggregate
  • kurtosis(): calculate the kurtosis from a statistical aggregate
  • sum(): calculate the sum from a statistical aggregate
  • num_vals(): get the number of values contained in a statistical aggregate

Rollup

  • rollup(): combine multiple one-dimensional statistical aggregates

Mutator

  • rolling(): create a rolling window aggregate for use in window functions