Question

I have a table that does not has any unique key or primary key. It has 50 columns and any or all of these columns can be duplicates. How do I delete all duplicate rows but keep the first occurrence?

Was it helpful?

Solution

The generic SQL approach is to store the data, truncate the table, and reinsert the data. The syntax varies a bit by database, but here is an example:

create table TempTable as
    select distinct * from MyTable;

truncate table MyTable;

insert into MyTable
    select * from TempTable;

There are other approaches that don't require a temporary table, but they are even more database-dependent.

OTHER TIPS

If you are using a mysql database use the following command

ALTER IGNORE TABLE tablename ADD UNIQUE INDEX (field1,field2,field3...) This allows duplicates to be removed through the addition of a unique index even with duplicate entries.(the IGNORE keyword is thus used)

If you are using an Oracle database use the following command

Delete from tablename where rowid not in (select min(rowid) from tablename group by row1,row2,row3.....)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top