Q
Problem solve Get help with specific problems with your technologies, process and projects.

Deleting all but one of the duplicate records

I want to delete duplicate records in table x, and I executed a query saying:

delete from x where col1 in (
 select col1 from x group by col1 having count(*)>1) 
It is deleting all the records in table that have duplicate col1 values, but my problem is that I want one record to stay.

Deleting records would not be a problem if you had a primary key on your table. The absence of a PK constraint leads to this exact problem. If you had a PK on the table, then you could delete by the PK columns to uniquely identify a row.

But a solution to your problem is not too difficult. This solution has elegantly been posted on Tom Kyte's AskTom Web site. Rather than duplicate the work, I'll give you the URL below for your solution:
http://asktom.oracle.com/pls/ask/f?p=4950:8:9843964383949885384::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:1224636375004,


This was last published in August 2004

Dig Deeper on Oracle database design and architecture

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close