RANK
Syntax
%%{init: {
'theme': 'base',
'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
'themeVariables': {
'fontSize': '11px',
'fontFamily': 'Arial'
}
}}%%
flowchart LR
Start((START)) --> RANK[RANK]:::quoted
RANK --> End((END))
Purpose
The RANK SQL function assigns a ranking number to each row based on the value of an expression. Separate ranking sequences can be introduced by partitioning on a key.
Rows with the same value receive the same rank. The next different value receives the next consecutive rank, while leaving gaps for duplicates of the same value. Rankings are calculated within each partition when a partition is specified.
See also ROW_NUMBER for numbering rows and DENSE_RANK for ranking without leaving gaps in the ranking.
Parameters:
- Current value expression (
ANY): the expression whose values are to be ranked. - Partition key (optional,
varchar2): the partition key. Defaults to''.
Returns: the rank as int32.
Examples
The following example retrieves the rank for each value without partitioning; note the gap in the ranking after the duplicate value a:
select c
, rank(c, null)
from csvtable
( passing 'a,a,b,c'
row delimiter ','
column delimiter '#'
columns c varchar2 position next
)
-------------------
a 1
a 1
b 3
c 4
The following example retrieves the rank per partitioned group:
select c
, grp
, rank(c, grp)
from csvtable
( passing 'a#GRP1,a#GRP1,a#GRP2,b#GRP1,c#GRP1,c#GRP2'
row delimiter ','
column delimiter '#'
columns c varchar2 position next
, grp varchar2 position next
)
-------------------
a GRP1 1
a GRP1 1
a GRP2 1
b GRP1 3
c GRP1 4
c GRP2 2