重复记录的SQL解决方案

时间:2012-12-10 15:24:54

标签: sql sql-server-2008-r2 duplicates

我正在使用SQL Server 2008 R2。我有一张桌子DESTI

CODE_DESTI   ENTERPRISE 
-----------------------
1            xx
1            yy
12           pp
12           oo
12           ll 

如何在

之后用自动增量1号更新重复的CODE_DESTI
CODE_DESTI   ENTERPRISE 
-----------------------
11            xx
12            yy
121           pp
122           oo
123           ll

1 个答案:

答案 0 :(得分:1)

您可以使用Window函数执行此操作:

SELECT
    CODE_DESTI + CAST(RANK() AS varchar) OVER (PARTITION BY CODE_DESTI ORDER BY ENTERPRISE) AS CODE_DESTI,
    ENTERPRISE
FROM DESTI