Skip to content

Instantly share code, notes, and snippets.

@abevieiramota
Created May 6, 2019 13:40
Show Gist options
  • Save abevieiramota/8d34297248b865b4103ecbd4b5ec6e31 to your computer and use it in GitHub Desktop.
Save abevieiramota/8d34297248b865b4103ecbd4b5ec6e31 to your computer and use it in GitHub Desktop.
create table censo_new (id int, nome text);
create table censo_old (id int, nome text);
insert into censo_new values (1, 'abelardo');
insert into censo_new values (2, 'fulano');
insert into censo_old values (1, 'abelardo vieira mota');
insert into censo_old values (2, 'fulano');
with row_1 as (
select a.id, j.*
from censo_old a,
jsonb_each(to_jsonb(a.*)) as j
), row_2 as (
select a.id, j.*
from censo_new a,
jsonb_each(to_jsonb(a.*)) as j
)
select row_1.id, row_1.key, row_1.value as old_value, row_2.value as new_value
from row_1
inner join row_2 using (id, key)
where row_1.value <> row_2.value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment