10

I have a table with some rows in. Every row has a date-field. Right now, it may be duplicates of a date. I need to delete all the duplicates and only store the row with the highest id. How is this possible using a SQL query?

Now:

date      id
'07/07'   1
'07/07'   2
'07/07'   3
'07/05'   4
'07/05'   5

What I want:

date      id
'07/07'   3
'07/05'   5
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267

3 Answers3

32
DELETE FROM table WHERE id NOT IN
    (SELECT MAX(id) FROM table GROUP BY date);
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
6

I don't have comment rights, so here's my comment as an answer in case anyone comes across the same problem:

In SQLite3, there is an implicit numerical primary key called "rowid", so the same query would look like this:

DELETE FROM table WHERE rowid NOT IN
(SELECT MAX(rowid) FROM table GROUP BY date);

this will work with any table even if it does not contain a primary key column called "id".

DaGaMs
  • 1,521
  • 17
  • 26
3

For mysql,postgresql,oracle better way is SELF JOIN.

Postgresql:
DELETE FROM table t1 USING table t2 WHERE t1.date=t2.date AND t1.id<t2.id;

MySQL        
DELETE FROM table
USING table, table as vtable
WHERE (table.id < vtable.id)
AND (table.date=vtable.date)

SQL aggregate (max,group by) functions almost always are very slow.

baklarz2048
  • 10,699
  • 2
  • 31
  • 37