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:- 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
Calculate basic statistics
This example calculates the average, standard deviation, and kurtosis over time buckets.-
Create the
measurementshypertable -
Insert sample data
Generate measurement data with varying distributions:
-
Calculate statistics over 10-minute buckets
Calculate rolling window statistics
Use window functions to calculate statistics over rolling time windows.-
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-dimensionalstats_agg performs linear regression on two variables.
-
Create the
measurements_multivalhypertable -
Insert correlated sample data
Generate data where val2 has a linear relationship with val1:
-
Calculate regression statistics
The two-dimensional aggregate calculates:
- Individual statistics for each variable (using
_yand_xsuffixes) - Linear regression parameters (slope, intercept)
- Correlation coefficient between the variables
- Individual statistics for each variable (using
- For more information about how aggregation works and how it inspired the two-step aggregation design, read our aggregation blog post.
- For technical details about two-step aggregation, see the developer documentation.