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
有帮助吗?

解决方案 2

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

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top