Sub topic 9
Filtered Aggregates as Window Functions
This chapter assumes you know basic SELECT, WHERE, ORDER BY, and aggregate functions such as count and sum. You should also understand that NULL means an unknown or missing value in SQL.
A window function calculates a value across related rows while keeping each original row in the result. A grouped aggregate returns one row per group. A windowed aggregate returns a value beside every row in that group.
PostgreSQL allows an aggregate window function to use FILTER (WHERE ...). The filter decides which rows contribute to the aggregate, while the OVER clause decides the window of rows available to it.
Running a filtered total
Assume the orders table has these columns:
id: the order identifiercustomer_id: the customer who placed the orderstatus: such aspaid,refunded, orpendingamount: the order valuecreated_at: the order timestamp
The following query calculates each customer's running paid total:
SELECT
id,
customer_id,
created_at,
status,
amount,
coalesce(
sum(amount) FILTER (WHERE status = 'paid')
OVER (
PARTITION BY customer_id
ORDER BY created_at, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
),
0
) AS running_paid_total
FROM orders
ORDER BY customer_id, created_at, id;PARTITION BY customer_id starts a separate calculation for each customer. The window is ordered by time, so each row sees the current order and earlier orders for that customer.
The FILTER clause means that only rows whose status is paid add their amount to the running total. Pending and refunded orders still appear in the result, but they contribute zero.
coalesce changes a NULL result to zero. For example, a customer's first few orders might all be pending. The filtered sum has no value to add yet, so PostgreSQL returns NULL unless you replace it with zero.
How the window is formed
The query's window has three important parts:
partition → order → frameA partition is the set of rows considered together. An order determines the sequence in which the window moves. A frame is the subset of the partition visible for the current row.
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW explicitly means “from the first row in this partition through this row.” The id tie-breaker makes the order deterministic when two orders have the same created_at value.
Without an explicit frame, PostgreSQL commonly uses a RANGE frame for an ordered window. Rows with equal ordering values can then share the same frame boundary. Use ROWS when you need one-row-at-a-time running behavior.
FILTER and WHERE do different jobs
A WHERE clause removes rows before the window calculation:
SELECT
id,
customer_id,
sum(amount) OVER (
PARTITION BY customer_id
ORDER BY created_at, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM orders
WHERE status = 'paid';This query returns only paid orders. Pending and refunded orders cannot appear, and they cannot be used by any other window calculation in this query.
By contrast, FILTER applies to one aggregate only. This lets one query show several running values while retaining every order:
SELECT
id,
customer_id,
status,
sum(amount) FILTER (WHERE status = 'paid')
OVER (
PARTITION BY customer_id
ORDER BY created_at, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_paid_total,
sum(amount) FILTER (WHERE status = 'refunded')
OVER (
PARTITION BY customer_id
ORDER BY created_at, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_refunded_total
FROM orders;Each aggregate has its own filter, but both use the same window definition.
A filtered aggregate window also differs from a grouped aggregate. GROUP BY customer_id would produce one row per customer. The windowed query keeps one row per order and adds calculations beside those rows.
Exercise
Which clause should you use if pending orders must remain in the result, but only paid orders should contribute to a running sum?
Show answer
Use FILTER (WHERE status = 'paid') on the aggregate window function. A WHERE clause would remove the pending rows from the result.