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
 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  

reacha
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

Devart
Posting Yak Master

102 Posts

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

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,

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

mma1979
Starting Member

Egypt
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

webfred
Flowing Fount of Yak Knowledge

Germany
8764 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
from
(select
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.11 seconds. Powered By: Snitz Forums 2000