Add example output for SQL window ranking functions
jstirnaman opened this issue · 0 comments
Thanks for pointing it out! These three functions are very similar when the values are different, but have slightly different behaviors when identical values are present. (I just learned this today!)
Both the rank
and dense_rank
repeat the ranking for the identical values. rank
skips ranks for identical values but dense_rank
continues. For example,
ID RANK ROW_NUMBER DENSE_RANK
-------- --------- --------------- ----------
1 1 1 1
1 1 2 1
1 1 3 1
2 4 4 2
I found a good example to show the difference between rank
, row_number
, and dense_rank
:
https://stackoverflow.com/questions/7747327/sql-rank-versus-row-number
Originally posted by @appletreeisyellow in #5854 (comment)
ROW_NUMBER : Returns a unique number for each row starting with 1. For rows that have duplicate values,numbers are arbitarily assigned.
Rank : Assigns a unique number for each row starting with 1,except for rows that have duplicate values,in which case the same ranking is assigned and a gap appears in the sequence for each duplicate ranking.