Name
DENSE_RANK
Synopsis
Computes a rank in a group for a hypothetical row that you supply. This is a dense rank. Rankings are never skipped, even when a group contains rows that rank identically.
SQL2003 Syntax
In the following syntax, items in the
value_list
correspond by position to items
in the sort_list
. Therefore, both lists
must have the same number of expressions.
DENSE_RANK(value_list
) WITHIN GROUP (ORDER BYsort_list
)value_list
::=expression
[,expression
...]sort_list
::=sort_item
[,sort_item
...]sort_item
::=expression
[ASC|DESC] [NULLS FIRST|NULLS LAST]
Oracle
Oracle follows the SQL2003 syntax and implements the following analytic syntax:
DENSE_RANK( ) OVER ([partioning
]ordering
)
For an explanation of the partioning
and
order
clauses, see the section later in
this chapter titled Section 4.3.
DB2, MySQL, PostgreSQl, and SQL Server
These platforms do not implement the DENSE_RANK aggregate function. However, DB2 does support DENSE_RANK as an analytic function. See the section later in this chapter titled Section 4.3.
Example
The following example determines the dense rank of the hypothetical
new row (num=4, odd=1)
within each group of rows
from test4, where groups are
distinguished by the values in the odd column:
SELECT * FROM test4;
NUM ODD ---------- ---------- 0 0 1 1 2 0 3 1 3 1 4 0 5 1SELECT odd, DENSE_RANK(4,1) WITHIN GROUP (ORDER BY num, odd)
FROM test4
GROUP BY odd;
ODD DENSE_RANK(4,1)WITHINGROUP(ORDERBYNUM,ODD) ---------- ------------------------------------------ 0 ...
Get SQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.