-
Book Overview & Buying
-
Table Of Contents
AI-Ready PostgreSQL 18
By :
Window functions differ significantly from groupings. A window function performs a calculation across a set of table rows that are somehow related to the current row. Window functions do not group rows into a single output row, unlike aggregate functions. They put a row into a context defined by a window over some or all the rows of the table.
For example, if we want to compare the price of a product with the average price of all products for the same brand, we place the current row, with the price of the current product, in the context of all rows for that brand.
In the following example, OVER (PARTITION BY brand) defines the window of rows (the context), with which we compare the current row – in that case, all the rows that refer to the same brand. This helps us put the price of an individual product into the context of the average prices for that brand:
SELECT DISTINCT brand, label, current_price AS price,
ROUND(AVG(current_price) OVER ...