Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created May 29, 2014 09:52
Show Gist options
  • Save chanmix51/dc44895e0490c1cd756a to your computer and use it in GitHub Desktop.
Save chanmix51/dc44895e0490c1cd756a to your computer and use it in GitHub Desktop.
Remove duplicate rows from a table
with
dup_id as (select id, count(*) from school group by id having count(*) > 1),
del_dup as (delete from only school using dup_id where school.id = dup_id.id returning school.*)
insert into school select distinct on (id) del_dup.* from del_dup order by del_dup.id returning *;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment