Overview
All window functions utilise a set of clauses specific for them, some of which are mandatory while others are optional.OVER Clause
When it comes to required ones, there is theOVER clause, which defines a window or user-specified set of rows within a query result set. It is a mandatory element of window functions, defining the window specification and differentiating them from other SQL functions.
Syntax
The syntax for this clause looks as follows:PARTITION BY clause is a list of expressions interpreted in much the same fashion as the elements of a GROUP BY clause, with major exception that they are always simple expressions and never the name or number of an output column. Another difference is that these expressions can contain aggregate function calls, which are not allowed in a regular GROUP BY clause (they are allowed here because windowing occurs after grouping and aggregation)
[ PARTITION BY expression [, ...] ] (optional window partition)
The ORDER BY clause used in the OVER clause above is a list of expressions interpreted in much the same fashion as the elements of a statement-level ORDER BY clause, except that the expressions are always taken as simple expressions and never the name or number of an output column.
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] (optional window ordering)
WINDOW Clause
In terms of window functions’ optional clauses, there is theWINDOW clause that defines one or more named window specification, as a window_name and window_definition pair.
Syntax
The syntax for this clause looks as follows:window_name is a name that can be referenced from the OVER clauses or subsequent window definition. There are a few important things to keep in mind here:
- The
window_definitionmay use anexisting_window_nameto refer to a previouswindow_definitionin theWINDOWclause, but the previouswindow_definitionmust not specify aframeclause - The
window_definitioncopies thePARTITION BYclause andORDER BYclause from previouswindow_definition, but it cannot specify its ownPARTION BYclause, and can specify anORDER BYclause if the previouswindow_definitiondoes not have one.
[ existing_window_name ] [ PARTITION BY clause ] [ ORDER BY clause ] [ frame clause ] (all arguments are optional)
The
window_definition without arguments defines a window with all rows without partition and orderingframe clause referenced above defines the window frame for window functions that depend on the frame (not all do).
The window frame is a set of related rows for each row of the query (called the current row).
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
The
frame clause of the window specification is limited to the ROWS clause without frame exclusion oneframe_startandframe_endcan be one of:UNBOUNDED PRECEDING,offset PRECEDING,CURRENT ROW,offset FOLLOWING,UNBOUNDED FOLLOWING.- If
frame_endis omitted it defaults toCURRENT ROW. Restrictions here are as follows:frame_startcannot beUNBOUNDED FOLLOWINGframe_endcannot beUNBOUNDED PRECEDINGframe_endchoice cannot appear earlier in the above list offrame_startandframe_endoptions than theframe_startchoice does
ROWS mode, CURRENT ROW means that the frame starts or ends with the current row, the offset is an integer indicating that the frame starts or ends that many rows before or after the current row.
Beware that the
ROWS mode can produce unpredictable results if the ORDER BY ordering does not order the rows uniquelyExamples
For the needs of this section, we will create thewinsales table that stores details about some sales transactions:
OVER Clause in Window Functions with Window Definition, PARTITION BY and ORDER BY clauses
In this example, we will focus on executing a window function with theOVER clause, window definition and PARTITION BY and ORDER BY clauses:
OVER Clause in Window Functions with Window Name, PARTITION BY and ORDER BY clauses
In this example, we will focus on executing a window function with theOVER clause, window name and PARITION BY and ORDER BY clauses: