Last active
December 14, 2015 01:09
-
-
Save Ocramius/5003948 to your computer and use it in GitHub Desktop.
SQL: Delete all but last items in groups of elements (grouped by common field) and incremental identifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE FROM | |
blah | |
WHERE | |
id NOT IN ( | |
SELECT | |
c.id | |
FROM | |
blah c | |
LEFT JOIN | |
blah d | |
ON ( | |
c.common_field = d.common_field | |
AND c.id < d.id | |
) | |
WHERE | |
d.id IS NUL | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment