Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Tables in BigQuery

How would I compare two tables (Table1 and Table2) and find all the new entries or changes in Table2.

Using SQL Server I can use

Select * from Table1
Except
Select * from Table2

Here a sample of what I want

Table1

 A   |  1
 B   |  2
 C   |  3

Table2

 A   |  1
 B   |  2
 C   |  2
 D   |  4

So, if I comparing the two tables I want my results to show me the following

C   |   2
D   |   4

I tried a few statements with no luck.

like image 271
user2917347 Avatar asked Feb 14 '23 21:02

user2917347


1 Answers

To get the differences (given that tkey is your unique row identifier):

SELECT a.tkey, a.name, b.name
FROM [your.tableold] a
JOIN EACH [your.tablenew] b
ON a.tkey = b.tkey
WHERE a.name != b.name
LIMIT 100

For the new rows, one way is the one you proposed:

SELECT col1, col2
FROM table2
WHERE col1 NOT IN
  (SELECT col1 FROM Table1)

(you'll have to switch to a JOIN EACH when Table1 gets too large)

like image 119
Felipe Hoffa Avatar answered Feb 27 '23 18:02

Felipe Hoffa