-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-duplicates
35 lines (27 loc) · 948 Bytes
/
remove-duplicates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--ways to remove duplicates data
--drop table #tech
--drop table #unique
create table #tech (id int identity (1,1),name varchar(50))
insert into #tech values ('C#')
insert into #tech values ('C#')
insert into #tech values ('C++')
insert into #tech values ('C++')
insert into #tech values ('F#')
insert into #tech values ('VB')
insert into #tech values ('C#')
insert into #tech values ('C#')
--#1 using temp table
select distinct name into #unique from #tech
delete from #tech
insert into #tech select * from #unique
select * from #tech
--#2 using ROW_NUMBER()
--select * from (select *, ROW_NUMBER() OVER(partition by name order by name ) as rn from #tech) dups
delete dups
from (select *, ROW_NUMBER() OVER(partition by name order by name ) as rn from #tech) dups
where rn >1
--#3 using CTE
;with cte
as (select ROW_NUMBER() OVER(partition by name order by (select 0 )) as rn from #tech)
delete from cte where rn > 1
select * from #tech