Update one of 2 duplicates in an sql server database table

前端 未结 11 1231
长发绾君心
长发绾君心 2020-12-05 00:48

I have got a table that got a column with duplicate values. I would like to update one of a 2 duplicate values so for example row1 = tom and row2 = tom

11条回答
  •  旧时难觅i
    2020-12-05 01:17

    I think this simple update is what you're looking for;

    UPDATE Table1 SET Column1=Column1+CAST(id AS VARCHAR)
    WHERE id NOT IN (
      SELECT MIN(id) 
      FROM Table1
      GROUP BY Column1
    );
    

    Input:

    (1,'A'),
    (2,'B'),
    (3,'A'),
    (4,'C'),
    (5,'C'),
    (6,'A');
    

    Output:

    (1,'A'),
    (2,'B'),
    (3,'A3'),
    (4,'C'),
    (5,'C5'),
    (6,'A6');
    

    An SQLfiddle to test with.

提交回复
热议问题