Step 1:- Create Temporary Table
create table #EmployeeRecord
(
EmpID int,
Emp_Name varchar(50),
)
insert into #EmployeeRecord(EmpID,Emp_Name)
select '1','Employee 1'
go 2
insert into #EmployeeRecord(EmpID,Emp_Name)
select '2','Employee 2'
go 2
insert into #EmployeeRecord(EmpID,Emp_Name)
select '3','Employee 3'
go
select * from #EmployeeRecord
Step 2:- Using Common Table Expressions(CTE)
;WITH CTE
AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY EmpID, Emp_Name ORDER BY (SELECT 0)) [RowNumber]
FROM #EmployeeRecord
)
DELETE FROM CTE
WHERE [RowNumber] > 1
Step 3:- Using Sub Queries
DELETE tbl
FROM (
SELECT *, Row_number() OVER (partition BY EmpID, Emp_Name ORDER BY EmpID DESC) [RowNumber]
FROM #EmployeeRecord
) tbl
WHERE [RowNumber] > 1
No comments:
Post a Comment