3
1

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 1 year has passed since last update.

ZOZOAdvent Calendar 2022

Day 10

BigQueryで構造体の各フィールドを展開したテーブルを作成する方法

Last updated at Posted at 2022-12-09

まず以下のような構造体からなるテーブルを考えます。

with t as (
  select struct(1 as hoge, 2 as fuga) as s union all
  select struct(3, 4) union all
  select struct(5, 6)
)

この構造体の各要素を列として取り出すにはどうすればよいでしょうか?
素直な実装は以下のように . で各要素を取り出すことです。
しかし、この方法では要素が多くなった時に煩雑です。

select s.hoge, s.fuga from t

BigQueryのSQLには select as value という記法があるので、これを使うと簡単に構造体の要素を展開できます。
この方法は構造体の要素名を明示的に指定していないので、要素の増減があっても変更する必要がないです。

select as value s from t
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?