declare @tmp table (id int, name varchar(20),sal int) --Template Table
insert into @tmp values(1,'AAAAA',52000)
insert into @tmp values(1,'AAAAA',52000)
insert into @tmp values(1,'AAAAA',52000)
insert into @tmp values(1,'AAAAA',52000)
insert into @tmp values(1,'AAAAA',52000)
select * from @tmp -- select query get the all recordsid name sal
1 AAAAA 52000
1 AAAAA 52000
1 AAAAA 52000
1 AAAAA 52000
1 AAAAA 52000
Now used Row_Number() get the numbers for each record
Syntax:
ROW_NUMBER ( ) OVER ( [ PARTITION BY value_expression , ... [ n ] ] order_by_clause )
Example :
;with tbl as (select ROW_NUMBER() over (order by id) as rownum,* from @tmp)
select * from tblRN id name sal
1 1 AAAAA 52000
2 1 AAAAA 52000
3 1 AAAAA 52000
4 1 AAAAA 52000
5 1 AAAAA 52000
--Delete the records
;with tbl as (select ROW_NUMBER() over (order by id) as rownum,* from @tmp)
delete from tbl where tbl.rownum<>1 --delete the duplicate records
select * from @tmpOutput:
RN id name sal
1 1 AAAAA 52000
