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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query to delete the duplicate rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

49 Posts

Posted - 11/04/2010 :  12:04:03  Show Profile  Reply with Quote
Can anyone help me with the query to delete duplicate rows from a table which does not have any identity column

Posting Yak Master

102 Posts

Posted - 11/05/2010 :  03:34:10  Show Profile  Visit Devart's Homepage  Reply with Quote

For example:

create table t_table(
f1 int,
f2 int,
f3 int

insert into t_table values(1,1,1);
insert into t_table values(2,2,2);
insert into t_table values(2,2,2);
insert into t_table values(2,2,2);
insert into t_table values(3,3,3);
insert into t_table values(3,3,3);
insert into t_table values(4,4,4);

declare @table table
(f1 int,f2 int, f3 int);
insert into @table
select f1,f2,f3 from t_table group by f1,f2,f3 having count(*)>1;

delete from t_table
from t_table t1
inner join @table t2
on t1.f1=t2.f1 and t1.f2=t2.f2 and t1.f3=t2.f3

insert into t_table
select f1,f2,f3 from @table;

Best regards,

SQL Server Tools:
dbForge Schema Compare
dbForge Data Compare
dbForge Query Builder
Go to Top of Page

Starting Member

1 Posts

Posted - 11/07/2010 :  06:30:01  Show Profile  Reply with Quote
thanks too much it a very usful to me

Mohammed Mohammed Abdelhay
Go to Top of Page

Flowing Fount of Yak Knowledge

8780 Posts

Posted - 11/07/2010 :  06:39:19  Show Profile  Visit webfred's Homepage  Reply with Quote
If you are using SQL Server 2005 or higher:
delete dt
row_number() over (partition by f1,f2,f3 order by f1) as rownum,
from t_table) as dt
where rownum > 1

edit: typo

No, you're never too old to Yak'n'Roll if you're too young to die.

Edited by - webfred on 11/07/2010 06:39:51
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