Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Remove duplicate records from a table in SQL Server

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 37
    Comment on it

    Sometimes we required to remove duplicate records from a table although table has a UniqueID Column with identity.


      Ex:   CREATE TABLE dbo.Employee
        ( 
        EmpID int IDENTITY(1,1) NOT NULL, 
        Name varchar(55) NULL, 
        Salary decimal(10, 2) NULL, 
        Designation varchar(20) NULL
         ) 
    


    Remove Duplicate Records by using ROW_NUMBER()


      WITH TempEmp (Name,duplicateRecCount)
        AS
        (
        SELECT Name,ROWNUMBER() OVER(PARTITION by Name, Salary ORDER BY Name) 
        AS duplicateRecCount
        FROM dbo.Employee
        )
        --Now Delete Duplicate Records
        DELETE FROM TempEmp
        WHERE duplicateRecCount > 1 
    
    SQL

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: