Please suppose that I have a table with a certain, unspecified number of columns.
I would like to delete the rows entirely duplicated, this means the rows which have all the columns matching in equality, leaving out the duplicated and keeping only 1 row.
How could I achieve this?
Example: SQL Server 2005, table with 5 columns FIELD1, FIELD2, FIELD3, FIELD4, FIELD5:
5 3 2 A J
3 5 2 A J
5 3 2 A J
5 3 2 A J
8 B 8 A K
The first row, the third row and the fourth row are duplicates, so you have to leave in the table only one of them.
You might employ row_number() to assign numbers to duplicates and delete where
rnis greater than one (single occurrance or first duplicate).A small test, since Sql Fiddle is not available ATM:
The downside is that you have to specify all columns. If you want to aviod this, and your table does not have foreign keys and is not referenced by any you might insert
DISTINCTdata into temporary table,TRUNCATEoriginal and reinsert from temporary table.