|
-
Sep 17th, 2003, 02:14 AM
#1
Thread Starter
Fanatic Member
Deleting duplicate rows
A very banal, a hackneyed issue, what's the query to delete duplicate rows from a table. For instance if my table has three rows with exactly the same data, then how do I delete two rows and just let one of them remain?
-
Sep 17th, 2003, 02:35 AM
#2
Do these rows are totally indentical or are is there (1 or more) field that is different (a PK = primary key). If there is no PK then it is not possible (I think) otherwise the query is possible:
VB Code:
select T1.ID , T2.ID from Table T1 where
inner join Table1 T2
T1.Field1 = T2.Field1
T1.Field2 = T2.Field2
T1.Field3 = T2.Field3
T1.ID > T2.ID
-
Sep 17th, 2003, 03:25 AM
#3
Addicted Member
don't know what DBMS you are using but for this i think i would:
Create a second table with a schema that matches the first table (the one containing duplicates)
Stick a unique constraint on one of the fields in the SECOND table.
Create a stored proc using the appropriate language - TSQL or PL/SQL or whatever. This proc should populate a cursor with the contents of table 1 and then loop through attempting to insert into table 2.
Catch the inevitable unique constraint exceptions in an error handling block, do nothing and then continue looping by using a resume statement, labels etc.
Cheers...
Just a thought - instead of the above why not just create the second table and populate it by using a SELECT DISTINCT * FROM TABLE_1 ?
Last edited by powdir; Sep 17th, 2003 at 03:29 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|