SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Removing duplicates from a table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Gigabyte
Starting Member

India
30 Posts

Posted - 06/14/2012 :  16:18:21  Show Profile  Reply with Quote
create table ForDuplicates (id int, name varchar(20))
insert into ForDuplicates values (1,'RAM'),(2,'LAXMAN'),(3,'YUDHI'),(1,'RAMA')
select * from ForDuplicates

Now I want to delete duplicates from the above tables.

Any suggestions??

Thanks in advance ! !



GIGABYTE+

yosiasz
Flowing Fount of Yak Knowledge

USA
1610 Posts

Posted - 06/14/2012 :  16:24:30  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote

declare @ForDuplicates table  (id int, name varchar(20))
insert into @ForDuplicates values (1,'RAM'),(2,'LAXMAN'),(3,'YUDHI'),(1,'RAMA')

select *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY id DESC)  Rn
 from @ForDuplicates
 
 
;with cteGig(id, name, rn)
AS
(
select *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY id DESC)  Rn
 from @ForDuplicates
 )
select * from cteGig where rn = 1


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3856 Posts

Posted - 06/14/2012 :  16:29:21  Show Profile  Reply with Quote
Assuming the ID represents the duplicate value, here is a DELETE statement based on Yasiasz code:
DELETE A
FROM
(
	select *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY id DESC)  Rn
	 from @ForDuplicates
 ) AS A
where rn <> 1
Go to Top of Page

Gigabyte
Starting Member

India
30 Posts

Posted - 06/14/2012 :  19:44:09  Show Profile  Reply with Quote
Thanks guys ...

It worked ....

GIGABYTE+
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000