Hycinth
Hycinth

Reputation: 65

How to get DENSE_RANK() results with RANK() in sql 2008

Category ID     Rank_Number     Rank_Number1
AA      x   1           1
AA      x   1           1
AA      x   1           1
AA      y   4           2
AA      y   4           2
AA      y   4           2
AA      z   7           3
AA      z   7           3
AA      z   7           3

I use SQL 2008 so I can’t use DENSE_RANK(). I’m using the following code, which gives me the Rank_Number. (not the way I want)

Select *,  RANK() OVER(PARTITION BY Category ORDER BY ID)    AS Rank_Number

My question:

How can I get the results like in Rank_Number1?

Upvotes: 0

Views: 101

Answers (1)

Szymon
Szymon

Reputation: 43023

DENSE_RANK() exists since SQL Server 2005:

Select *,  DENSE_RANK() OVER(PARTITION BY Category ORDER BY ID)    
from table1
AS Rank_Number

Upvotes: 1

Related Questions