SQL aggregation window functions are powerful tools for performing calculations across a set of rows related to the current row within a query result. They allow you to perform aggregate functions (e.g., sum, average, count) while still retaining individual row-level data. Window functions are distinct from regular aggregate functions in that they do not cause rows to become grouped into a single output row.
Here are some common SQL aggregation window functions:
ROW_NUMBER():
Assigns a unique sequential integer to each row within the partition defined by the OVER() clause.
Useful for generating row numbers or ranking rows within a partition.
SELECT Â Â Â Â ROW_NUMBER() OVERÂ (ORDERÂ BYÂ column_name) ASÂ row_num, Â Â Â Â column1, column2 FROM Â Â Â Â table_name; |
RANK() and DENSE_RANK():
Assigns a rank to each row based on the ordering specified in the OVER() clause. RANK() leaves gaps in rank values for tied rows, while DENSE_RANK() does not.
Useful for ranking rows based on a particular criterion.
SELECT Â Â Â Â RANK() OVERÂ (ORDERÂ BYÂ column_name) ASÂ rank, Â Â Â Â column1, column2 FROM Â Â Â Â table_name; |
NTILE():
Divides the result set into a specified number of equally sized groups (tiles) and assigns a bucket number to each row.
Useful for percentile analysis or partitioning data into equal-sized groups.
SELECT Â Â Â Â NTILE(n) OVERÂ (ORDERÂ BYÂ column_name) ASÂ bucket, Â Â Â Â column1, column2 FROM Â Â Â Â table_name; |
LEAD() and LAG():
Allow access to data from subsequent rows (LEAD) or preceding rows (LAG) within the same result set.
Useful for calculating differences or trends between current and neighboring rows.
SELECT Â Â Â Â column_name, Â Â Â Â LEAD(column_name) OVERÂ (ORDERÂ BYÂ ordering_column) ASÂ next_value, Â Â Â Â LAG(column_name) OVERÂ (ORDERÂ BYÂ ordering_column) ASÂ prev_value FROM Â Â Â Â table_name; |
SUM(), AVG(), COUNT(), MIN(), MAX()Â with window frames:
These are standard aggregate functions, but when used with window frames, they calculate aggregates over a specific subset of rows defined by the frame clause.
Useful for calculating running totals, moving averages, or cumulative aggregates.
SELECT Â Â Â Â column_name, Â Â Â Â SUM(column_name) OVERÂ (ORDERÂ BYÂ ordering_column ROWSÂ BETWEENÂ 2Â PRECEDING ANDÂ CURRENTÂ ROW) ASÂ running_sum FROM Â Â Â Â table_name; |
Window functions provide powerful capabilities for analytical queries in SQL, enabling complex calculations and analyses without sacrificing row-level details. They are widely supported in modern relational database management systems like PostgreSQL, SQL Server, Oracle, and others.
Comments