Thursday, 8 May 2014

Remove duplicate rows from Sql Server

-- Create Table with Name "Employee"
CREATE TABLE dbo.Employee
(
EmpID int IDENTITY(1,1) NOT NULL,
Name varchar(55) NULL,
Mobile varchar(50) NULL
 )

 -- Insert recodrs into "Employee" Table
 INSERT INTO dbo.Employee (Name,Mobile)
 Select 'Vikas','8010554741'
 Select 'Vikas','8010554741'
 Select 'Vijay','5827419631'
 Select 'Vijay','5827419631'


-- Code to Delete Duplicate Records
WITH TempEmployee (Name,EmpCount)
AS
(
SELECT Name,ROW_NUMBER() OVER(PARTITION by Name, Mobile ORDER BY Name)
AS EmpCount
FROM dbo.Employee
)

--Delete Duplicate Records
DELETE FROM TempEmployee

WHERE EmpCount > 1

No comments:

Post a Comment