Created
August 16, 2018 21:54
-
-
Save thieux/859814aef21baabc867691467eace7d8 to your computer and use it in GitHub Desktop.
Sample of an inline update from a reconciliation query on PostgreSQL 10
This file contains hidden or 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
create table bar (id int, top int, extid varchar) | |
insert into bar values | |
(1, 0, 'MC1234'), | |
(2, 1, 'FR1234'), | |
(3, 1, 'MC9876'), | |
(4, 0, 'MC5678') | |
commit | |
-- reconciliate rows MC1234 and FR1234 | |
select * | |
from bar a | |
join bar b | |
on substring(b.extid, 3, 4) = substring(a.extid, 3, 4) | |
and substring(b.extid, 1, 2) <> substring(a.extid, 1, 2) | |
and substring(a.extid, 1, 2) = 'FR' | |
-- inline update | |
update bar | |
set top = a.top | b.top | |
from bar a | |
join bar b | |
on substring(b.extid, 3, 4) = substring(a.extid, 3, 4) | |
and substring(b.extid, 1, 2) <> substring(a.extid, 1, 2) | |
and substring(a.extid, 1, 2) = 'FR' | |
where bar.id = b.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment