LoginSignup
15
21

More than 1 year has passed since last update.

テーブル内データのdiff

Last updated at Posted at 2014-01-13

2020-04-24追記
2021-08-23URL修正
より深く考察されている記事を見つけました。
BigQuery テーブル同士の一致判定 | Queuery


集合演算可能な2つのテーブル(カラムの数と型が同じテーブル)の差異を取得する。

例えば、対象テーブル(foo)の、ある時点のデータを別テーブル(foo_backup)として保存しておき、その内容の差異を取得する。

方法1

(
    select * foo
    union all
    select * foo_backup
)
except all
(
    select * foo
    intersect all
    select * foo_backup
)

方法2

(
    select * foo
    except all
    select * foo_backup
)
union all
(
    select * foo_backup
    except all
    select * foo
)
  • 方法2の方が対称的で覚えやすいかも
  • allを忘れると重複行が1行になってしまうので注意

ベン図を描かないと理解しにくい、、、。

2020.7.14追記

BigQueryでは EXCEPT ALL が使えないので、代わりに EXCEPT DISTINCT を使う。
ただし当然、2つのテーブル間で同値行の行数が異なる場合でも、1行として扱われてしまうので差異がないことになってしまう。

15
21
1

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
15
21