SQL存储过程重复记录

时间:2018-06-29 11:55:25

标签: sql sql-server

假设我有重复的记录,我想在第一个记录中添加1,为所有重复的记录添加0,如何实现呢? ?

1 个答案:

答案 0 :(得分:1)

喜欢吗?

UPDATE t1
SET [Column1] = IIF(sub.[RowNumber] = 1, 1, 0)
FROM Table1 AS t1
    INNER JOIN (
        SELECT [ID], ROW_NUMBER() OVER(PARTITION BY [Column2] ORDER BY [ID]) AS [RowNumber]
        FROM Table1
    ) AS sub
        ON sub.[ID] = t1.[ID];

--[Column1] For Update
--[Column2] Your Duplicates