How to delete duplicate rows in SQL Server?

后端 未结 23 1441
长情又很酷
长情又很酷 2020-11-22 00:58

How can I delete duplicate rows where no unique row id exists?

My table is

col1  col2 col3 col4 col5 col6 col7
john  1          


        
23条回答
  •  遇见更好的自我
    2020-11-22 01:48

    Oh wow, i feel so stupid by ready all this answers, they are like experts' answer with all CTE and temp table and etc.

    And all I did to get it working was simply aggregated the ID column by using MAX.

    DELETE FROM table WHERE col1 IN (
        SELECT MAX(id) FROM table GROUP BY id HAVING ( COUNT(col1) > 1 )
    )
    

    NOTE: you might need to run it multiple time to remove duplicate as this will only delete one set of duplicate rows at a time.

提交回复
热议问题