Window functions let ParadeDB calculate running totals, rankings, and other analytics across related rows without collapsing them into groups.
Window functions answer questions like running customer spend, order ranking, or stock percentage without losing row-level detail. They operate on a “window” of rows defined by PARTITION BY and ORDER BY clauses.
Write SELECT columns, add a function_name() OVER (PARTITION BY … ORDER BY …) AS alias, then reference the table. Each OVER clause sets the window frame.
ROW_NUMBER(), RANK(), DENSE_RANK(), SUM(), AVG(), LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE(), and NTILE() cover most analytics needs.
SELECT id,
customer_id,
total_amount,
RANK() OVER (PARTITION BY customer_id ORDER BY total_amount DESC) AS order_rank
FROM Orders;
SELECT id,
customer_id,
order_date,
total_amount,
SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS running_spend
FROM Orders;
ROWS counts physical rows; RANGE counts value ranges. Use ROWS for true running totals and RANGE for time-bucketed analytics.
Without ORDER BY PostgreSQL treats all rows in a partition as peers, making rankings unpredictable. Explicit ordering guarantees reproducible results.
Window functions run after WHERE but before HAVING. Apply base filters in WHERE to avoid unnecessary computation.
Yes, OVER w AS (...) lets you name a window once and reuse it across many functions to keep queries readable and performant.
Select aggregate results in a subquery, then apply window functions in the outer query, or use FILTER() within the window to isolate rows.
Yes. Wrap the query in a subquery or CTE, then filter on the alias in the outer SELECT.
They can if partitions are large. Index the PARTITION BY and ORDER BY columns to help PostgreSQL sort efficiently.
Yes. Create a view with the window logic; ParadeDB materializes it on-the-fly while remaining fully updatable if no aggregates change underlying keys.