How can I delete duplicate rows in a table

后端 未结 13 1353
情歌与酒
情歌与酒 2020-12-08 22:30

I have a table with say 3 columns. There\'s no primary key so there can be duplicate rows. I need to just keep one and delete the others. Any idea how to do this is Sql Serv

相关标签:
13条回答
  • 2020-12-08 23:29

    Here's the method I used when I asked this question -

    DELETE MyTable 
    FROM MyTable
    LEFT OUTER JOIN (
       SELECT MIN(RowId) as RowId, Col1, Col2, Col3 
       FROM MyTable 
       GROUP BY Col1, Col2, Col3
    ) as KeepRows ON
       MyTable.RowId = KeepRows.RowId
    WHERE
       KeepRows.RowId IS NULL
    
    0 讨论(0)
提交回复
热议问题