Skip to content

Instantly share code, notes, and snippets.

@igorpronin
Created January 23, 2017 11:51
Show Gist options
  • Save igorpronin/c61d14f6b81a9a39fddfdf4874a468b3 to your computer and use it in GitHub Desktop.
Save igorpronin/c61d14f6b81a9a39fddfdf4874a468b3 to your computer and use it in GitHub Desktop.
Удаление дубликатов из таблицы c primary key
-- http://stackoverflow.com/questions/1746213/how-to-delete-duplicate-entries
-- Given table table, want to unique it on (field1, field2) keeping the row with the max field3:
DELETE FROM table USING table alias
WHERE table.field1 = alias.field1 AND table.field2 = alias.field2 AND
table.max_field < alias.max_field
-- For example, I have a table, user_accounts, and I want to add a unique constraint on email, but I have some duplicates. Say also that I want to keep the most recently created one (max id among duplicates).
DELETE FROM user_accounts USING user_accounts ua2
WHERE user_accounts.email = ua2.email AND user_account.id < ua2.id;
-- Note - USING is not standard SQL, it is a PostgreSQL extension (but a very useful one), but the original question specifically mentions PostgreSQL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment