---
title: Time-weighted averages and integrals | Tiger Data Docs
description: Use time-weighted averages and integrals for time-series data that is not evenly sampled
---

When samples land at uneven intervals, a simple mean can overweight short spikes or underweight long plateaus. **Time-weighted averages** and integrals assign each reading weight proportional to how long it was in effect, so irregular sampling still reflects duration correctly. Evenly spaced series (for example every 30 seconds) rarely need this; irregular bursts or adaptive sampling do.

For example, if you have a lot of ice cream in freezers, you need to make sure the ice cream stays within a 0-10℉ (-20 to -12℃) temperature range. The temperature in the freezer can vary if folks are opening and closing the door, but the ice cream only has a problem if the temperature is out of range for a long time. You can set your sensors in the freezer to sample every five minutes while the temperature is in range, and every 30 seconds while the temperature is out of range. If the results are generally stable, but with some quick moving transients, an average of all the data points weights the transient values too highly. A time weighted average weights each value by the duration over which it occurred based on the points around it, producing much more accurate results.

Time weighted integrals are useful when you need a time-weighted sum of irregularly sampled data. For example, if you bill your users based on irregularly sampled CPU usage, you need to find the total area under the graph of their CPU usage. You can use a time-weighted integral to find the total CPU-hours used by a user over a given time period.

- For more information about how time-weighted averages work, read the [time-weighted averages blog](https://www.tigerdata.com/blog/what-time-weighted-averages-are-and-why-you-should-care).
- For more information about time-weighted average API calls, see the [hyperfunction API documentation](/docs/reference/toolkit/time_weight/time_weight/index.md).

## Run a time-weighted average query

Time weighted average in TimescaleDB is implemented as an aggregate that weights each value using last observation carried forward (LOCF), or linear interpolation. The aggregate is not parallelizable, but it is supported with [continuous aggregates](/docs/build/continuous-aggregates/create-a-continuous-aggregate/index.md).

In this procedure, we use an example table called `freezer_temps` that contains data about internal freezer temperatures.

1. **Find the average and the time-weighted average**

   ```
   SELECT freezer_id,
     avg(temperature),
    average(time_weight('Linear', ts, temperature)) as time_weighted_average
   FROM freezer_temps
   GROUP BY freezer_id;
   ```

2. **Use a time-weighted average in a window function**

   To determine if the freezer has been out of temperature range for more than 15 minutes at a time:

   ```
   SELECT *,
   average(
           time_weight('Linear', ts, temperature) OVER (PARTITION BY freezer_id ORDER BY ts RANGE  '15 minutes'::interval PRECEDING )
          ) as rolling_twa
   FROM freezer_temps
   ORDER BY freezer_id, ts;
   ```

For more information about time-weighted average API calls, see the [hyperfunction API documentation](/docs/reference/toolkit/time_weight/time_weight/index.md).
