SQL Reference

OLAP Functions

 
OLAP-function
 
|--+-| ranking-function |-----+---------------------------------|
   +-| numbering-function |---+
   '-| aggregation-function |-'
 
ranking-function
 
|---+-RANK ()-------+--OVER------------------------------------->
    '-DENSE_RANK ()-'
 
>----(--+------------------------------+------------------------>
        '-| window-partition-clause |--'
 
>----| window-order-clause |--)---------------------------------|
 
numbering-function
 
|---ROW_NUMBER ()--OVER---(--+------------------------------+--->
                             '-| window-partition-clause |--'
 
>----+--------------------------+---)---------------------------|
     '-| window-order-clause |--'
 
aggregation-function
 
|---column-function--OVER---(--+------------------------------+->
                               '-| window-partition-clause |--'
 
>----+--------------------------+------------------------------->
     '-| window-order-clause |--'
 
     .-RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING--.
>----+------------------------------------------------------------+---)->
     '-| window-aggregation-group-clause |------------------------'
 
>---------------------------------------------------------------|
 
window-partition-clause
 
                  .-,--------------------------.
                  V                            |
|---PARTITION BY-----partitioning-expression---+----------------|
 
window-order-clause
 
              .-,-------------------------------.
              V                       .-ASC--.  |
|---ORDER BY-----sort-key-expression--+------+--+---------------|
                                      '-DESC-'
 
window-aggregation-group-clause
 
|---+-ROWS--+---+-| group-start |---+---------------------------|
    '-RANGE-'   '-| group-between |-'
 
group-start
 
|---+-UNBOUNDED PRECEDING-----------+---------------------------|
    +-unsigned-constant--PRECEDING--+
    '-CURRENT ROW-------------------'
 
group-between
 
|---BETWEEN--| group-bound1 |--AND--| group-bound2 |------------|
 
group-bound1
 
|---+-UNBOUNDED PRECEDING-----------+---------------------------|
    +-unsigned-constant--PRECEDING--+
    +-unsigned-constant--FOLLOWING--+
    '-CURRENT ROW-------------------'
 
group-bound2
 
|---+-UNBOUNDED FOLLOWING-----------+---------------------------|
    +-unsigned-constant--PRECEDING--+
    +-unsigned-constant--FOLLOWING--+
    '-CURRENT ROW-------------------'
 

On-Line Analytical Processing (OLAP) functions provide the ability to return ranking, row numbering and existing column function information as a scalar value in a query result. An OLAP function can be included in expressions in a select-list or the ORDER BY clause of a select-statement (SQLSTATE 42903). An OLAP function cannot be used as an argument of a column function (SQLSTATE 42607). The query result to which the OLAP function is applied is the result table of the innermost subselect that includes the OLAP function.

When specifying an OLAP function, a window is specified that defines the rows over which the function is applied, and in what order. When used with a column function, the applicable rows can be further refined, relative to the current row, as either a range or a number of rows preceding and following the current row. For example, within a partition by month, an average can be calculated over the previous three month period.

The ranking function computes the ordinal rank of a row within the window. Rows that are not distinct with respect to the ordering within their window are assigned the same rank. The results of ranking may be defined with or without gaps in the numbers resulting from duplicate values.

If RANK is specified, the rank of a row is defined as 1 plus the number of rows that strictly precede the row. Thus, if two or more rows are not distinct with respect to the ordering, then there will be one or more gaps in the sequential rank numbering.

If DENSE_RANK 36 is specified, the rank of a row is defined as 1 plus the number of rows preceding that are distinct with respect to the ordering. Therefore, there will be no gaps in the sequential rank numbering.

The ROW_NUMBER 37 function computes the sequential row number of the row within the window defined by the ordering, starting with 1 for the first row. If the ORDER BY clause is not specified in the window, the row numbers are assigned to the rows in arbitrary order as returned by the subselect (not according to any ORDER BY clause in the select-statement).

The data type of the result of RANK, DENSE_RANK or ROW_NUMBER is BIGINT. The result cannot be null.

PARTITION BY (partitioning-expression,...)
Defines the partition within which the function is applied. A partitioning-expression is an expression used in defining the partitioning of the result set. Each column-name referenced in a partitioning-expression must unambiguously reference a result set column of the OLAP function subselect statement (SQLSTATE 42702 or 42703). The length of each partitioning-expression must not be more than 255 bytes (SQLSTATE 42907). A partitioning-expression cannot include a scalar-fullselect (SQLSTATE 42822) or any function that is not deterministic or has an external action (SQLSTATE 42845).

ORDER BY (sort-key-expression,...)
Defines the ordering of rows within a partition that determine the value of the OLAP function or the meaning of the ROW values in the window-aggregation-group-clause (it does not define the ordering of the query result set). A sort-key-expression is an expression used in defining the ordering of the rows within a window partition. Each column-name referenced in a sort-key-expression must unambiguously reference a column of the result set of the subselect including the OLAP function (SQLSTATE 42702 or 42703). The length of each sort-key-expression must not be more than 255 bytes (SQLSTATE 42907). A sort-key-expression cannot include a scalar fullselect (SQLSTATE 42822) or any function that is not deterministic or has an external action (SQLSTATE 42845). This clause is required for the RANK and DENSE_RANK functions (SQLSTATE 42601).

ASC
Uses the values of the sort-key-expression in ascending order. Null values are considered last in the order.

DESC
Uses the values of the sort-key-expression in descending order. Null values are considered first in the order.

window-aggregation-group-clause
The aggregation group of a row R is a set of rows, defined relative to R in the ordering of the rows of R's partition. This clause specifies the aggregation group.

ROWS
Indicates the aggregation group is defined by counting rows.

RANGE
Indicates the aggregation group is defined by an offset from a sort key.

group-start
Specifies the starting point for the aggregation group. The aggregation group end is the current row. Specification of the group-start clause is equivalent to a group-between clause of the form "BETWEEN group-start AND CURRENT ROW".

group-between
Specifies the aggregation group start and end based on either ROWS or RANGE.

UNBOUNDED PRECEDING
Includes the entire partition preceding the current row. This can be specified with either ROWS or RANGE. Also, this can be specified with multiple sort-key-expressions in the window-order-clause.

UNBOUNDED FOLLOWING
Includes the entire partition following the current row. This can be specified with either ROWS or RANGE. Also, this can be specified with multiple sort-key-expressions in the window-order-clause.

CURRENT ROW
Specifies the start or end of the aggregation group as the current row. This clause cannot be specified in group-bound2 if group-bound1 specifies value FOLLOWING.

value PRECEDING
Specifies either the range or number of rows preceding the current row. If ROWS is specified, then value is a positive integer indicating a number of rows. If RANGE is specified, then the data type of value must be comparable to the type of the sort-key-expression of the window-order-clause. There can only be one sort-key-expression, and the data type of the sort-key-expression must allow subtraction. This clause cannot be specified in group-bound2 if group-bound1 is CURRENT ROW or value FOLLOWING.

value FOLLOWING
Specifies either the range or number of rows following the current row. If ROWS is specified, then value is a positive integer indicating a number of rows. If RANGE is specified, then the data type of value must be comparable to the type of the sort-key-expression of the window-order-clause. There can only be one sort-key-expression, and the data type of the sort-key-expression must allow addition.

Examples:


Footnotes:

36
DENSE_RANK and DENSERANK are synonyms.

37
ROW_NUMBER and ROWNUMBER are synonyms.


[ Top of Page | Previous Page | Next Page ]