MDAC 2.5 SDK - OLE DB Programmer's Reference
Chapter 26: Mapping MDX to SQL Statements
Both the order of tuples in the sets processed by MDX and the placement level are important for many OLAP operations.
To preserve order, a RANK column must be generated for every set. This column contains ordering information. The first tuple has a rank value of 1, the second has a rank value of 2, and so on.
Rank values are generated by an extension to SQL: the RANK clause. This clause occurs at the end of a query expression (such as a SELECT statement). The input to this clause is a table, and its output is also a table. The syntax is as follows:
<rank_clause> ::=
query_expression
RANK ROWS | LEVELS [AS <column_name>]
[RANKORDER BY <sort_specification_list>]
[RANKGROUP BY <grouping_column_reference_list>]
The semantics of this clause are as follows:
Many OLAP operations require that a set of input tuples be separated into groups and that the top x rows be picked from each group (according to some criteria). As always, it is required that the ordering be preserved. The RANKTOP clause has the ability to pick the top x rows.
This clause occurs at the end of a query expression (that is, a SELECT statement). The input to this clause is a table, and its output is also a table. The syntax is as follows:
<ranktop_clause> ::=
<query_expression>
RANKTOP <numeric_value_expression> ROWS
| LEVELS [AS <column_name>]
[RANKORDER BY <sort_specification_list>]
[RANKGROUP BY <grouping_column_reference_list>]
The semantics of this clause are as follows:
Both RANK and RANKTOP clauses are part of a query expression; their output is a table. Therefore, if a query expression has more than one RANK or RANKTOP clause, they are evaluated in the order of appearance. The first clause results in a table that is used as the input to the second clause, which is in turn used as an input to the third clause, and so on.
For example, consider the following query:
SELECT Name, Newrank AS Rank
FROM
(((SELECT Name, "1" AS Src, Rank FROM S1
UNION [ALL]
SELECT Name, "2" AS Src, Rank FROM S2)
RANKTOP 1 ROWS RANKORDER BY Src, Rank RANKGROUP BY Name)
RANK ROWS AS NewRank RANKORDER BY Src, Rank)
ORDER BY Rank
In above example, the innermost query expression is used as an input to the RANKTOP clause. The output of the RANKTOP clause, which is again a table (indented two levels above), is used as an input to the RANK clause. The output of the RANK clause is also a table (indented one level above), which is used as input to the FROM clause.