Skip to content

Instantly share code, notes, and snippets.

@sebastianwebber
Created August 27, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianwebber/7b4ab352733e8aab8b03f94d8aa0c7a9 to your computer and use it in GitHub Desktop.
Save sebastianwebber/7b4ab352733e8aab8b03f94d8aa0c7a9 to your computer and use it in GitHub Desktop.
compare rows on different tables (with the same structure)
test=# \pset null UNKNOW
Null display is "UNKNOW".
test=# create table employee1(id serial primary key, name text);
CREATE TABLE
test=# create table employee2(id serial primary key, name text);
CREATE TABLE
test=# -- load the fake data on employee1
test=# insert into employee1 (name) SELECT '1: Person '|| generate_series(1,10);
INSERT 0 10
test=# -- load the fake data on employee2
test=# insert into employee2 select * from employee1;
INSERT 0 10
test=# -- fix the sequence
test=# alter sequence employee2_id_seq start 11;
test=# -- now, compare the data
test=# SELECT
e1.id as e1_pk,
e2.id as e2_pk,
COALESCE(e1.* = e2.*, false) as equal
FROM employee1 as e1
RIGHT JOIN employee2 as e2 USING (id);
test=# -- insert new data on e2
test=# insert into employee2 (name) SELECT '2: People '|| generate_series(1,4);
INSERT 0 4
test=# -- now, it WILL be diferente
test=# SELECT
e1.id as e1_pk,
e2.id as e2_pk,
COALESCE(e1.* = e2.*, false) as equal
FROM employee1 as e1
RIGHT JOIN employee2 as e2 USING (id);
e1_pk | e2_pk | equal
--------+-------+-------
1 | 1 | t
2 | 2 | t
3 | 3 | t
4 | 4 | t
5 | 5 | t
6 | 6 | t
7 | 7 | t
8 | 8 | t
9 | 9 | t
10 | 10 | t
UNKNOW | 11 | f
UNKNOW | 12 | f
UNKNOW | 13 | f
UNKNOW | 14 | f
(14 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment