122 High-Function Business Intelligence in e-business
A brief explanation of some of the OLAP functions follows, with examples
provided as appropriate.
RANK
The RANK function assigns a sequential rank of a row within a window.
The RANK of a row is defined as one plus the number of rows that strictly
precede the row.
Rows that are not distinct within the ordering of the window are assigned equal
ranks.
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. That is, the results of RANK
may have gaps in the numbers resulting from duplicate values.
DENSE_RANK
Like the RANK function, DENSE_RANK assigns a sequential rank to a row in a
window. However, its DENSE_RANK is one plus the number of rows preceding it
that are distinct with respect to the ordering. Therefore, there will be no gaps in
the sequential rank numbering, with ties being assigned the same rank.
ROWNUMBER
ROWNUMBER computes the sequential row number of the row within the
window defined by an ordering clause (if one is specified), starting with 1 for the
first row and continuing sequentially to the last row in the window.
If an ordering clause, ORDER BY, is not specified in the window, the row
numbers are assigned to the rows in arbitrary order as returned by the
sub-select.
PARTITION BY
The PARTITION BY clause allows for subdividing the window into partitions. A
partitioning-expression
is used to define the partitioning of the result set.
ORDER BY
The ORDER BY clause defines the ordering of rows within a window that
determines the value of the OLAP function or the meaning of the ROW values in
the window-aggregation-group-clause (see the following section concerning the
window-aggregation-group).
The ORDER BY clause does not define the ordering of the query result set.