sql - How to compare two tables in Postgresql? -
i have 2 identical tables:
a : id1, id2, qty, unit b: id1, id2, qty, unit the set of (id1,id2) identifying each row , can appear once in each table.
i have 140 rows in table a , 141 rows in table b. find keys (id1,id2) not appearing in both tables. there 1 sure there can't more (for example if each table has whole different data).
i wrote query:
(table except table b) union (table b except table a) ; but it's not working. compares whole table don't care if qty or unit different, care id1,id2.
use full outer join:
select a.*,b.* full outer join b on a.id1=b.id1 , a.id2=b.id2 this show both tables side side. gaps there unmatched row.
select a.*,b.* full outer join b on a.id1=b.id1 , a.id2=b.id2 a.id1 null or b.id1 null; that show unmatched rows.
or can use not in
select * (id1,id2) not in ( select id1,id2 b ) that show rows not matched b.
or same result using join
select a.* left outer join b on a.id1=b.id1 , a.id2=b.id2 b.id1 null sometimes join faster "not in"
Comments
Post a Comment