How to delete completely duplicate rows

后端 未结 4 1694
孤街浪徒
孤街浪徒 2021-01-18 05:31

Say i have duplicate rows in my table and well my database design is of 3rd class :-

Insert Into tblProduct (ProductId,ProductName,Description,Category) Valu         


        
4条回答
  •  无人及你
    2021-01-18 05:51

    Try this - it will delete all duplicates from your table:

    ;WITH duplicates AS
    (
        SELECT 
           ProductID, ProductName, Description, Category,
           ROW_NUMBER() OVER (PARTITION BY ProductID, ProductName
                              ORDER BY ProductID) 'RowNum'
        FROM dbo.tblProduct
    )
    DELETE FROM duplicates
    WHERE RowNum > 1
    GO
    
    SELECT * FROM dbo.tblProduct
    GO
    

    Your duplicates should be gone now: output is:

    ProductID   ProductName   DESCRIPTION        Category
       1          Cinthol         cosmetic soap      soap
       1          Lux             cosmetic soap      soap
       1          Crowning Glory  cosmetic soap      soap
       2          Cinthol         nice soap          soap
       3          Lux             nice soap          soap
    

提交回复
热议问题