How to delete completely duplicate rows

后端 未结 4 1731
孤街浪徒
孤街浪徒 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:53

    I had to do this a few weeks back... what version of SQL Server are you using? In SQL Server 2005 and up, you can use Row_Number as part of your select, and only select where Row_Number is 1. I forget the exact syntax, but it's well documented... something along the lines of:

    Select t0.ProductID, 
           t0.ProductName, 
           t0.Description, 
           t0.Category
    Into   tblCleanData
    From   (
        Select ProductID, 
               ProductName, 
               Description, 
               Category, 
               Row_Number() Over (
                   Partition By ProductID, 
                                ProductName, 
                                Description, 
                                Category
                   Order By     ProductID,
                                ProductName,
                                Description,
                                Category
               ) As RowNumber
        From   MyTable
    ) As t0
    Where t0.RowNumber = 1
    

    Check out http://msdn.microsoft.com/en-us/library/ms186734.aspx, that should get you going in the right direction.

提交回复
热议问题