Created
February 15, 2014 20:36
-
-
Save seriwb/9024901 to your computer and use it in GitHub Desktop.
someidが同じで、fooidがXXXから始まるレコードが複数あれば、pkが若いレコード1件だけ残して削除するSQL
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 <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