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

# average()

> Calculate the time-weighted average of values in a TimeWeightSummary

<Icon icon="tag" iconType="duotone" /> Since [1.0.0][toolkit-1.0.0]

Calculate the time-weighted average. Equal to [`integral`][integral] divided by the elapsed time. Note that there is a
key difference to `avg()`: If there is exactly one value, `avg()` would return that value, but `average()` returns
`NULL`.

## Samples

Calculate the time-weighted average of the column `val`, using the 'last observation carried forward' interpolation
method:

```sql theme={"dark"}
SELECT
    id,
    average(tws)
FROM (
    SELECT
        id,
        time_weight('LOCF', ts, val) AS tws
    FROM foo
    GROUP BY id
) t
```

## Arguments

The syntax is:

```sql theme={"dark"}
average(
    tws TimeWeightSummary
) RETURNS DOUBLE PRECISION
```

| Name | Type              | Default | Required | Description                                            |
| ---- | ----------------- | ------- | -------- | ------------------------------------------------------ |
| tws  | TimeWeightSummary | -       | ✔        | The input TimeWeightSummary from a time\_weight() call |

## Returns

| Column  | Type             | Description                |
| ------- | ---------------- | -------------------------- |
| average | DOUBLE PRECISION | The time-weighted average. |

[integral]: /api-reference/timescaledb-toolkit/time_weight/integral

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