MAX window function
The MAX window function returns the maximum of the input expression values. The MAX function works with numeric values and ignores NULL values.
Syntax
MAX ( [ ALL ] expression ) OVER ( [ PARTITION BY expr_list ] [ ORDER BY order_list frame_clause ] )
Arguments
- expression
-
The target column or expression that the function operates on.
- ALL
-
With the argument ALL, the function retains all duplicate values from the expression. ALL is the default. DISTINCT is not supported.
- OVER
-
A clause that specifies the window clauses for the aggregation functions. The OVER clause distinguishes window aggregation functions from normal set aggregation functions.
- PARTITION BY expr_list
-
Defines the window for the MAX function in terms of one or more expressions.
- ORDER BY order_list
-
Sorts the rows within each partition. If no PARTITION BY is specified, ORDER BY uses the entire table.
- frame_clause
-
If an ORDER BY clause is used for an aggregate function, an explicit frame clause is required. The frame clause refines the set of rows in a function's window, including or excluding sets of rows within the ordered result. The frame clause consists of the ROWS keyword and associated specifiers. See Window function syntax summary.
Data types
Accepts any data type as input. Returns the same data type as expression.
Examples
The following example shows the sales ID, quantity, and maximum quantity from the beginning of the data window:
select salesid, qty, max(qty) over (order by salesid rows unbounded preceding) as max from winsales order by salesid; salesid | qty | max ---------+-----+----- 10001 | 10 | 10 10005 | 30 | 30 10006 | 10 | 30 20001 | 20 | 30 20002 | 20 | 30 30001 | 10 | 30 30003 | 15 | 30 30004 | 20 | 30 30007 | 30 | 30 40001 | 40 | 40 40005 | 10 | 40 (11 rows)
For a description of the WINSALES table, see Sample table for window function examples.
The following example shows the salesid, quantity, and maximum quantity in a restricted frame:
select salesid, qty, max(qty) over (order by salesid rows between 2 preceding and 1 preceding) as max from winsales order by salesid; salesid | qty | max ---------+-----+----- 10001 | 10 | 10005 | 30 | 10 10006 | 10 | 30 20001 | 20 | 30 20002 | 20 | 20 30001 | 10 | 20 30003 | 15 | 20 30004 | 20 | 15 30007 | 30 | 20 40001 | 40 | 30 40005 | 10 | 40 (11 rows)