How to delete duplicate rows in SQL Server?

后端 未结 23 1841
长情又很酷
长情又很酷 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:46

    Another way of removing dublicated rows without loosing information in one step is like following:

    delete from dublicated_table t1 (nolock)
    join (
        select t2.dublicated_field
        , min(len(t2.field_kept)) as min_field_kept
        from dublicated_table t2 (nolock)
        group by t2.dublicated_field having COUNT(*)>1
    ) t3 
    on t1.dublicated_field=t3.dublicated_field 
        and len(t1.field_kept)=t3.min_field_kept
    

提交回复
热议问题