Question

How to change the old sequence in the column to new in using sql server 2005

old new
   1   1
   1   1
   3   2
   4   3
   5   4
   5   4
   5   4
   8   5
   8   5
   10  6
Était-ce utile?

La solution 2

Use the window function dense_rank() without a PARTITION clause:

SELECT old, dense_rank() OVER (ORDER BY old) AS new
FROM   tbl;

Autres conseils

I think you're looking for dense_rank:

select dense_rank() over (order by old) rn, old
from yourtable
order by old

Are you looking for DENSE_RANK()

SELECT
    T.*
    ,DENSE_RANK() OVER (ORDER BY T.Old) As New
FROM Table1 T

Fiddle Demo

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top