60
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

2つのテーブルの差分をBigQueryで求めてみます。

テーブルをFULL OUTER JOINで結合して、排他的論理和を求めても良いですが、テーブルのすべての列をON句の後ろに書く必要があり、そこそこ面倒です。
https://www.codeproject.com/articles/33052/visual-representation-of-sql-joins

というわけで、 except を使って差分を出してみました。
このクエリの結果が0行であれば、2つのテーブルはすべての行が一致しています。

#standardSQL
with table1 as (
  SELECT 1 as a, 2 as b, 3 as c union all
  SELECT 2 as a, 4 as b, 6 as c
), table2 as (
  SELECT 1 as a, 2 as b, 3 as c
)

select * from (select * from table1 except distinct select * from table2) union all
select * from (select * from table2 except distinct select * from table1)

except は左のクエリに存在しているが右のクエリに存在しない行を返す関数です。
そのため、 select * from table1 except distinct select * from table2 はtable1に存在するがtable2には存在しない行を返します。
あとは、このクエリのtable1とtable2を逆にしたクエリも用意し、それらを union で繋げば完成です。

60
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
60
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?