Neoview SQL Reference Manual (R2.5)
MAX Window Function
MAX is a window function that returns the maximum value of all non null values of the given
expression for the current window specified by the inline-window-specification.
MAX ([ALL] expression) OVER (inline-window-specification)
inline-window-specification is:
[PARTITION BY expression [, expression]...]
[ORDER BY expression [ASC[ENDING] | DESC[ENDING]]
[,expression [ASC[ENDING] | DESC[ENDING]]]...]
[ window-frame-clause ]
window-frame-clause is:
ROWS CURRENT ROW
| ROWS preceding-row
| ROWS BETWEEN preceding-row AND preceding-row
| ROWS BETWEEN preceding-row AND CURRENT ROW
| ROWS BETWEEN preceding-row AND following-row
| ROWS BETWEEN CURRENT ROW AND CURRENT ROW
| ROWS BETWEEN CURRENT ROW AND following-row
| ROWS BETWEEN following-row AND following-row
preceding-row is:
UNBOUNDED PRECEDING
| unsigned-integer PRECEDING
following-row is:
UNBOUNDED FOLLOWING
| unsigned-integer FOLLOWING
ALL
specifies whether duplicate values are included in the computation of the MAX of the
expression. The default option is ALL, which causes duplicate values to be included.
expression
specifies an expression that determines the values over which the MAX is computed.
See “Expressions” (page 255).
inline-window-specification
specifies the window over which the MAX is computed. The
inline-window-specification can contain an optional PARTITION BY clause, an
optional ORDER BY clause and an optional window frame clause. The PARTITION BY clause
specifies how the intermediate result is partitioned and the ORDER BY clause specifies how
the rows are ordered within each partition.
window-frame-clause
specifies the window within the partition over which the MAX is computed.
Examples of MAX Window Function
• Return the running maximum of the SALARY column:
SELECT empnum, MAX(salary)
OVER (ORDER BY empnum ROWS UNBOUNDED PRECEDING)
FROM persnl.employee;
• Return the running maximum of the SALARY column within each department:
SELECT deptnum, empnum, MAX(salary)
OVER (PARTITION BY deptnum ORDER BY empnum ROWS UNBOUNDED PRECEDING)
FROM persnl.employee;
• Return the moving maximum of salary within each department over a window of the last
4 rows:
498 OLAP Functions