Delete duplicate rows in sql
Create table Employees
(
ID int,
Name nvarchar(50),
Gender nvarchar(50),
Salary int
)
GO
Insert into Employees values (1,'Ram', 'Male', 70000)
Insert into Employees values (1,'Ram', 'Male', 70000)
Insert into Employees values (1,'Ram', 'Male', 70000)
Insert into Employees values (2,'Ram', 'Male', 70000)
Insert into Employees values (2,'Ram', 'Male', 70000)
Insert into Employees values (2,'Ram', 'Male', 70000)
Insert into Employees values (3,'Priya', 'Female', 35000)
Insert into Employees values (3,'Priya', 'Female', 35000)
Query
WITH EmployeesCTE AS
(
SELECT *, ROW_NUMBER()OVER(PARTITION BY ID ORDER BY ID) ASRowNumber
FROM Employees
)
DELETE FROM EmployeesCTE WHERE RowNumber > 1
No comments:
Post a Comment