Window functions provide the ability to perform calculations across sets of rows that are related to the current query row. Note that these functions must be invoked using window function syntax, i.e., an OVER clause is required. The built-in window functions are listed below.
Function | Return type | Description | Available since |
---|---|---|---|
cume_dist(value any, boolean, boolean) | double | cumulative distribution: (number of partition rows preceding or peer with current row) / total partition rows | Apr2019 (11.33.3) |
dense_rank(value any, boolean, boolean) | int | rank of the current row without gaps | |
diff(value any) | boolean | is the current row value different from other partition rows | |
diff(boolean, value any) | boolean | is the current row value different from other partition rows | |
first_value(value any) | any | returns value evaluated at the row that is the first row of the window frame | Apr2019 (11.33.3) |
lag(value any, [offset int, [default any,]] boolean, boolean) | same type as value | returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null | Apr2019 (11.33.3) |
last_value(value any) | same type as value | returns value evaluated at the row that is the last row of the window frame | Apr2019 (11.33.3) |
lead(value any, [offset int, [default any,]] boolean, boolean) | same type as value | returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null | Apr2019 (11.33.3) |
nth_value(value any, nth int) | same type as value | returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row | Apr2019 (11.33.3) |
ntile(value any, num_buckects int, boolean, boolean) | int | integer ranging from 1 to the argument value, dividing the partition as equally as possible | Apr2019 (11.33.3) |
percent_rank(value any, boolean, boolean) | double | relative rank of the current row: (rank - 1) / (total partition rows - 1) | Apr2019 (11.33.3) |
rank(value any, boolean, boolean) | int | rank of the current row with gaps | |
row_number(value any, boolean, boolean) | int | number of the current row within its partition, counting from 1 |
In addition to above functions, some aggregate functions can be used as a window function. Aggregate functions act as window functions only when an OVER clause follows the call; otherwise they act as non-window aggregates and return a single row for the entire set.
When an aggregate function is used as a window function, it aggregates over the rows within the current row's window frame. An aggregate used with ORDER BY and the default window frame definition produces a “running sum” type of behavior, which may or may not be what's wanted. To obtain aggregation over the whole partition, omit ORDER BY or use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. Other frame specifications can be used to obtain other effects.
See also Window Functions syntax and examples. See also [Extended SQL:2011 Window Functions in MonetDB release Apr2019 You can also create your own window functions, see Create Window Function command.
To query the available window functions in your MonetDB server run query:
SELECT DISTINCT schema_id, name, func, mod, language, system FROM sys.functions WHERE type = 6 ORDER BY name;