window functions
Window functions allow you to perform calculations across a set of table rows that are somehow related to the current row, similar to aggregate functions,…
Overview
Window functions allow you to perform calculations across a set of table rows that are somehow related to the current row, similar to aggregate functions, but without grouping the rows into a single output row. This lets you compare and analyze values while maintaining the individual rows of your data.
Key Concepts
A window function operates on a "window" - a set of rows determined by the OVER clause. The window can be defined using PARTITION BY to group rows, ORDER BY to sequence them, and optional frame clauses to limit which rows are included in the calculation.
Common Use Cases
Window functions excel at calculating running totals, finding ranks within groups, comparing values to previous/next rows, and computing moving averages. They're especially valuable for time-series analysis and financial calculations.
DuckDB Examples
Basic ranking example:
Copy code
SELECT
product_name,
sales_amount,
RANK() OVER (ORDER BY sales_amount DESC) as sales_rank
FROM sales;
Partitioned ranking by category:
Copy code
SELECT
category,
product_name,
sales_amount,
RANK() OVER (PARTITION BY category ORDER BY sales_amount DESC) as category_rank
FROM sales;
Running total with frame clause:
Copy code
SELECT
date,
amount,
SUM(amount) OVER (
ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) as running_total
FROM transactions;
Moving average:
Copy code
SELECT
date,
temperature,
AVG(temperature) OVER (
ORDER BY date
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) as moving_avg_3day
FROM weather_data;
DuckDB-Specific Features
DuckDB supports named windows, which can make complex window function queries more readable:
Copy code
SELECT
date,
amount,
SUM(amount) OVER w1 as running_total,
AVG(amount) OVER w1 as running_avg
FROM transactions
WINDOW w1 AS (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
DuckDB also allows window functions to be used in UPDATE statements and supports a wide range of window frame specifications, including GROUPS mode alongside the standard ROWS and RANGE modes.
Related terms
The OVER clause turns an aggregate or ranking function into a window function by defining the partition, order, and frame it operates on, without collapsing the result set.
Window frame →A window frame is the subset of rows within a window function's partition that a calculation is applied to, defined with ROWS, RANGE, or GROUPS relative to the current row.
SQL function →A SQL function is a reusable piece of code that performs a specific operation and returns a result.
aggregate functions →Aggregate functions are SQL operations that perform calculations across multiple rows of data to produce a single summary value.
PARTITION BY →PARTITION BY divides rows into independent groups for a window function or an output operation, so calculations reset for each group without collapsing rows the way GROUP BY does.
ROW_NUMBER →ROW_NUMBER() is a SQL window function that assigns a unique, sequential integer to each row within a partition based on a specified order, with no ties.
