Skip to content

Instantly share code, notes, and snippets.

@seriwb
Created February 15, 2014 20:36
Show Gist options
  • Save seriwb/9024901 to your computer and use it in GitHub Desktop.
Save seriwb/9024901 to your computer and use it in GitHub Desktop.
someidが同じで、fooidがXXXから始まるレコードが複数あれば、pkが若いレコード1件だけ残して削除するSQL
delete from <table> t1
where t1.<someid> is not null
and t1.<pk> > (
select min(t2.<pk>)
from <table> t2
where (t1.<someid> = t2.<someid> or (t1.<someid> is null and t2.<someid> is null))
and t1.<fooid> like 'XXX%'
having count(*) > 1
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment