sql - How do I force the rank function to assign a different value to rows of equal rank? -
i have table in data base similar one
id year quarter ..... 1 2000 3 2 2002 4 1 2003 3 1 2000 3
the key data base combination of id, year, , quarter. need grab recent entry each id, used rank function one:
select id ( select id, year, quarter rank() on (partition id order year + quarter) rank mytable ) tmp rank = 1
it possible there entry in data base has same id, year, , quarter. need able choose 1 of them none of others. don't particularly care one, not more one. have been reading through msdn pages , can't seem find looking for. have ideas? thank you.
there 3 basic options - in case, others noted, should use row_number()
.
value row_number rank dense_rank 1 1 1 2 1 1 b 3 3 2 d 4 4 3 e 5 5 4
basically, row_number
assigns rows 1 number, , not duplicate or skip values (unless using partition). rank
allows ties, , skips values if there tie previous value. dense_rank
allows ties, , not skip values.
Comments
Post a Comment