# Compute a moving average

Shape / table

A variant of [Calculate across rows](https://meridian.online/operations/calculate-across-rows).

Aggregate over a window of a fixed number of rows, or a span of time, that slides along with the current row.

Page: <https://meridian.online/operations/calculate-across-rows/moving-average>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

average over a sliding window of three rows and over the last seven days

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
CREATE TEMP TABLE result AS
SELECT order_id, order_date, amount,
       round(avg(amount) OVER (ORDER BY order_date, order_id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 4) AS avg_3_rows,
       round(avg(amount) OVER (ORDER BY order_date RANGE BETWEEN INTERVAL 6 DAYS PRECEDING AND CURRENT ROW), 4) AS avg_7_days
FROM orders
WHERE amount IS NOT NULL;
SELECT * FROM result ORDER BY order_date, order_id;
SELECT CASE WHEN ((SELECT list(avg_3_rows ORDER BY order_date, order_id) FROM result) = [120.0, 100.25, 166.8333, 141.9167, 285.0833, 218.4133, 373.33, 208.33, 248.3333] AND (SELECT list(avg_7_days ORDER BY order_date, order_id) FROM result) = [120.0, 166.8333, 166.8333, 277.625, 277.625, 99.99, 510.0, 262.5, 220.0]) THEN 'as expected' ELSE error('the three-row and seven-day moving averages were not the expected values') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- EasyMorph: [`Sliding aggregation`](https://help.easymorph.com/doku.php?id=transformations:slidingaggregation) (read 2026-09-27)
- KNIME Analytics Platform: [`Moving Aggregator`](https://api.hub.knime.com/nodes/*JKPnK49gCJ_6tVwe?details=full) (read 2026-09-27)
- KNIME Analytics Platform: [`Moving Average`](https://api.hub.knime.com/nodes/*i6a4eZBqe7EBhz9G?details=full) (read 2026-09-27)
- Apache NiFi: [`AttributeRollingWindow`](https://nifi.apache.org/components/org.apache.nifi.processors.stateful.analysis.AttributeRollingWindow/) (read 2026-09-27)
- Polars: [`DataFrame.rolling`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`Expr.ewm_mean`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.ewm_mean_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.ewm_std`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.ewm_sum`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.ewm_sum_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.ewm_var`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling`](https://docs.pola.rs/api/python/stable/reference/expressions/window.html) (read 2026-09-27)
- Polars: [`Expr.rolling_kurtosis`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_map`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_max`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_max_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_mean`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_mean_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_median`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_median_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_min`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_min_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_quantile`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_quantile_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_rank`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_rank_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_skew`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_std`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_std_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_sum`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_sum_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_var`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.rolling_var_by`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`rolling_corr`](https://docs.pola.rs/api/python/stable/reference/expressions/functions.html) (read 2026-09-27)
- Polars: [`rolling_cov`](https://docs.pola.rs/api/python/stable/reference/expressions/functions.html) (read 2026-09-27)
- RapidMiner Studio: [`Moving Average Filter`](https://docs.rapidminer.com/latest/studio/operators/modeling/time_series/transformation/moving_average_filter.html) (read 2026-09-27)
- Tableau Prep: [`Moving Calculation`](https://help.tableau.com/current/prep/en-us/prep_multirow_calculations.htm) (read 2026-09-27)
- Trifacta: [`ROLLINGAVERAGE Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGCOUNTA Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGKTHLARGEST Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGKTHLARGESTUNIQUE Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGLIST Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGMAX Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGMAXDATE Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGMIN Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGMINDATE Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGMODE Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGMODEDATE Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGSTDEV Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGSTDEVSAMP Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGSUM Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGVAR Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`ROLLINGVARSAMP Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- VisiData: [`addcol-window`](https://github.com/saulpw/visidata/blob/7609212ec2febafa9caba7183cbebfaf49627909/visidata/features/window.py#L63) (read 2026-09-23)
