lundi 27 avril 2015

SQL Deleting all but N duplicate values

I can't seem to figure out how to delete all but some duplicate values.

Code like this works fine for deleting all the duplicates:

DELETE FROM table1 WHERE id NOT IN 
(SELECT MAX(id) FROM table1 GROUP BY name);

But I'm stuck on how to make this work so that it keeps more than just one row, such as the second and third largest id values along with the largest.

Aucun commentaire:

Enregistrer un commentaire