Skip to main content
On this page

Kater SQL Functions Calculation context

MOVING_AVERAGE

Averages a value over this row and the rows before it, in the order you give.

Family: Calculation context

Syntax

MOVING_AVERAGE(value, rows => n) OVER ([PARTITION BY ...] ORDER BY ...)

Arguments

ArgumentDescription
valueThe number to average: a measure or an expression.
rowsHow many rows to average, this row included, such as 3.

Returns

The average of this row and the rows before it.

Usage notes

  • The first rows have fewer rows before them, so their average uses fewer rows.
  • Write OVER with ORDER BY after the call. It counts rows, not calendar periods.

Example

This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.

SELECT
orders.order_month AS "Order Month",
EVAL(orders.sum__quantity) AS "Units",
MOVING_AVERAGE(orders.sum__quantity, rows => 3) OVER (ORDER BY orders.order_month) AS "Units, 3-Month Average"
FROM kater_dataset.orders AS orders
WHERE
KATER__IS_ON_OR_AFTER(orders.order_date, DATE '2026-05-01')
GROUP BY
orders.order_month
ORDER BY
1
Order MonthUnitsUnits, 3-Month Average
2026-05-01441441.0
2026-06-01403422.0
2026-07-01438427.33
2026-08-01454431.67
2026-09-01442444.67

See also