Database & SQL

Moderators: None (Apply to moderate this forum)
Number of threads: 1194
Number of posts: 2247

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sql for deleting duplicate rows Posted by Raj19 on 1 Feb 2002 at 4:46 AM
Hi,
How can I delete duplicate rows , keeping one copy of each, from a table which has no primary / unique key. My idea is to get rid of redundant records which were inserted unchecked in the beginning.
Report
Re: sql for deleting duplicate rows Posted by gigsvoo on 3 Feb 2002 at 10:56 PM
DELETE Table1
WHERE (SELECT Count(*) From Table1 WHERE Count(*) > 1)

Neo Gigs
http://communities.msn.com.my/VisualBasicwithNeo

Report
Re: sql for deleting duplicate rows Posted by MrEd on 18 Feb 2002 at 7:31 PM
: DELETE Table1
: WHERE (SELECT Count(*) From Table1 WHERE Count(*) > 1)
:
: Neo Gigs
: http://communities.msn.com.my/VisualBasicwithNeo
:
:

umm, thats not going to work. It will delete both duplicate rows, where im assuming he wants to leave one of them. Try something like:

delete from table1 a
where rowid > (
select min(rowid)
from table1 b
where a.keys = b.keys
);

I think this is oracle specific though.
Report
Re: sql for deleting duplicate rows Posted by ryan_j_smith on 26 Mar 2002 at 10:16 AM
This isn't going to accomplish what you want and it is Oracle specific.
You have to first decide what columns in the table make a duplicate row and then join on ALL of those columns ( in the example col1 and col2 ). This solution is Oracle specific. If you need it for another database you will need to place a primary key on the table ( which makes the query easier ) and then only delete those rows where one primary key is greater or less than another.

delete from MyTable o
where
exists (
select * from MyTable i
where
o.col1 = i.col1 and
o.col2 = i.col2 and
... and
o.rowid < i.rowid )
);

: umm, thats not going to work. It will delete both duplicate rows, where im assuming he wants to leave one of them. Try something like:
:
: delete from table1 a
: where rowid > (
: select min(rowid)
: from table1 b
: where a.keys = b.keys
: );
:
: I think this is oracle specific though.
:

Report
Re: sql for deleting duplicate rows Posted by sumedh on 29 Mar 2002 at 5:17 AM
: This isn't going to accomplish what you want and it is Oracle specific.
: You have to first decide what columns in the table make a duplicate row and then join on ALL of those columns ( in the example col1 and col2 ). This solution is Oracle specific. If you need it for another database you will need to place a primary key on the table ( which makes the query easier ) and then only delete those rows where one primary key is greater or less than another.
:
: delete from MyTable o
: where
: exists (
: select * from MyTable i
: where
: o.col1 = i.col1 and
: o.col2 = i.col2 and
: ... and
: o.rowid < i.rowid )
: );
:
: : umm, thats not going to work. It will delete both duplicate rows, where im assuming he wants to leave one of them. Try something like:
: :
: : delete from table1 a
: : where rowid > (
: : select min(rowid)
: : from table1 b
: : where a.keys = b.keys
: : );
: :
: : I think this is oracle specific though.
: :

just get the 'rowid' of the duplicate rows u want to delete:

select rowid from <table>;


and then use the DELETE command to remove a row with that rowid.....
stupid solution.....but it works......
dont do this if u have a lot of records...

-sumedh





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.