stats_agg (one variable) overview
Statistical analysis functions for one-dimensional data
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
Section titled “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:
- More efficient because multiple accessors can reuse the same aggregate
- Easier to reason about performance, because aggregation is separate from final computation
- Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
- 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
Section titled “Samples”Calculate statistical properties
Section titled “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
Section titled “Available functions”Aggregate
Section titled “Aggregate”stats_agg(): aggregate data into an intermediate statistical aggregate form for further calculation
Accessors
Section titled “Accessors”average(): calculate the average from a statistical aggregatestddev(): calculate the standard deviation from a statistical aggregatevariance(): calculate the variance from a statistical aggregateskewness(): calculate the skewness from a statistical aggregatekurtosis(): calculate the kurtosis from a statistical aggregatesum(): calculate the sum from a statistical aggregatenum_vals(): get the number of values contained in a statistical aggregate
Rollup
Section titled “Rollup”rollup(): combine multiple one-dimensional statistical aggregates
Mutator
Section titled “Mutator”rolling(): create a rolling window aggregate for use in window functions