Skip to content

Instantly share code, notes, and snippets.

@thieux
Created August 16, 2018 21:54
Show Gist options
  • Save thieux/859814aef21baabc867691467eace7d8 to your computer and use it in GitHub Desktop.
Save thieux/859814aef21baabc867691467eace7d8 to your computer and use it in GitHub Desktop.
Sample of an inline update from a reconciliation query on PostgreSQL 10
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