Skip to content

Instantly share code, notes, and snippets.

@fweep
Created March 13, 2013 00:16
Show Gist options
  • Save fweep/5148336 to your computer and use it in GitHub Desktop.
Save fweep/5148336 to your computer and use it in GitHub Desktop.
Clean up duplicate rows in a PostgreSQL table with differing IDs
-- Expensive sub-query method:
delete from questions_tags qt using questions_tags qt2 where qt.question_id = qt2.question_id and qt.tag_id = qt2.tag_id and qt.id != qt2.id and qt.id not in (select min(id) from questions_tags qt3 where qt.question_id = qt3.question_id and qt.tag_id = qt3.tag_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment