Skip to main content
Statistical aggregation provides efficient ways to calculate common statistical measures like averages, standard deviations, and kurtosis. These aggregates work seamlessly with continuous aggregates and window functions, making it easy to analyze time-series data at different time scales and perform rolling calculations. The statistical aggregation functions in use a two-step aggregation process. First, you create an aggregate with stats_agg(), which produces an intermediate form that can be efficiently stored and re-aggregated. Second, you apply accessor functions like average(), stddev(), or kurtosis() to extract the final values. This design makes it straightforward to combine aggregates, work with continuous aggregates, and perform complex rolling window calculations. Statistical aggregates are available in both one-dimensional and two-dimensional forms. The two-dimensional form enables linear regression analysis by tracking the relationship between dependent and independent variables.

Prerequisites

To follow the steps on this page:
  • Create a target with Real-time analytics enabled.

    You need your connection details. This procedure also works for .

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.

Calculate basic statistics

This example calculates the average, standard deviation, and kurtosis over time buckets.
  1. Create the measurements hypertable
  2. Insert sample data Generate measurement data with varying distributions:
  3. Calculate statistics over 10-minute buckets

Calculate rolling window statistics

Use window functions to calculate statistics over rolling time windows.
  1. Calculate 15-minute rolling statistics This query first aggregates data into 1-minute buckets, then uses a window function to calculate rolling 15-minute statistics:

Perform linear regression

The two-dimensional stats_agg performs linear regression on two variables.
  1. Create the measurements_multival hypertable
  2. Insert correlated sample data Generate data where val2 has a linear relationship with val1:
  3. Calculate regression statistics The two-dimensional aggregate calculates:
    • Individual statistics for each variable (using _y and _x suffixes)
    • Linear regression parameters (slope, intercept)
    • Correlation coefficient between the variables