Skip to main content
On this page

Kater SQL Functions Windows

KATER__WINDOW_AGGREGATION

A running or trailing aggregate of the value, row by row in the OVER's order.

Family: Windows

Syntax

KATER__WINDOW_AGGREGATION(field, function, frame [, size]) OVER ([PARTITION BY ...] ORDER BY ...)

Arguments

Required

ArgumentDescription
fieldThe value it reads on each row: a field, or a measure, such as orders.sum__amount.
functionThe aggregate to take of the rows in the frame. One of 'count', 'count_distinct', 'min', 'max', 'sum', 'avg'.
frame'running' takes every row from the first to this one. 'trailing' takes this row and the rows before it, size rows in all. One of 'running', 'trailing'.

Optional

ArgumentDescription
sizeHow many rows a trailing frame holds, this row included. A running frame does not read it. A whole number. It is 3 when left out.

Returns

A number. The field menu shows it as the aggregate it takes: a count as a whole number, and the others in the format of the field it reads.

Usage notes

  • Write OVER (ORDER BY ...), and add PARTITION BY to start again for each group. It writes its own frame, so its OVER takes no ROWS or RANGE.
  • A running frame compiles to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, and a trailing frame of size 3 to ROWS BETWEEN 2 PRECEDING AND CURRENT ROW.
  • It counts rows, not calendar periods, so a month with no row is not in the frame.
  • A running sum of a measure that cannot be added up across a dimension, such as Revenue across Currency, must start over for each value. Put that dimension in PARTITION BY.

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",
CUSTOM_CALCULATION(
'window_aggregation__sum__quantity',
KATER__WINDOW_AGGREGATION(orders.sum__quantity, 'sum', 'running') OVER (ORDER BY orders.order_month)
) AS "Units So Far"
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 So Far
2026-05-01441441
2026-06-01403844
2026-07-014381,282
2026-08-014541,736
2026-09-014422,178

Compiles to

The call compiles to the same SQL on DuckDB and on Snowflake.

SUM(orders.sum__quantity) OVER (ORDER BY orders.order_month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

See also