Abe Miessler
Abe Miessler

Reputation: 85086

How to include a row number but show a tie?

Say I have the query below:

WITH TEMP AS
  (
     select 1 as id, 4 as value
     UNION
     SELECT 2, 53
     UNION
     SELECT 3, 1
     UNION
     SElECT 4, 474
     UNION
     SELECT 5, 53
  )

  SELECT *,
  ROW_NUMBER() OVER (ORDER BY value)
  FROM TEMP

This returns the following:

3   1   1
1   4   2
2   53  3
5   53  4
4   474 5

I would like two rows with 53 to have the same row number (3) and the final row to keep it's row number of 5. I assume this cannot be done using ROW_NUMBER. Can anyone point me in the right direction to get me started on this?

Upvotes: 6

Views: 1971

Answers (1)

user596075
user596075

Reputation:

Instead of using ROW_NUMBER, you want to use RANK.

  SELECT *,   
      RANK() OVER (ORDER BY value)   
  FROM TEMP

RANK (T-SQL) MSDN Reference

Upvotes: 8

Related Questions