> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 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][pg-stats-aggs], 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][stats_agg-2d].

## 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][blog-two-step-aggregates].

[blog-two-step-aggregates]: https://www.timescale.com/blog/how-postgresql-aggregation-works-and-how-it-inspired-our-hyperfunctions-design

## 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:

```sql theme={"dark"}
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()`][stats_agg]: aggregate data into an intermediate statistical aggregate form for further calculation

### Accessors

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

### Rollup

* [`rollup()`][rollup]: combine multiple one-dimensional statistical aggregates

### Mutator

* [`rolling()`][rolling]: create a rolling window aggregate for use in window functions

[average]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/average

[kurtosis]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/kurtosis

[num_vals]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/num_vals

[rolling]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/rolling

[rollup]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/rollup

[skewness]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/skewness

[stats_agg]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/stats_agg

[stats_agg-2d]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-two-variables

[stddev]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/stddev

[sum]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/sum

[toolkit-1.3.0]: https://github.com/timescale/timescaledb-toolkit/releases/tag/1.3.0

[two-step-aggregation]: #two-step-aggregation

[pg-stats-aggs]: https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-STATISTICS-TABLE

[variance]: /api-reference/timescaledb-toolkit/statistical-and-regression-analysis/stats_agg-one-variable/variance
