LoginSignup
15
5

More than 5 years have passed since last update.

Bigqueryで変数を使う

Posted at

はじめに

他のDBでは使えるものもあるようですが、Bigqueryでは変数が使えません。
使えないですが、クエリを工夫すると変数のようなものを扱うことができ、
再利用性も可読性もすごく高まります。

今回はBQでよくある、パーティションタイムだけ都度変えてクエリを発行する
という場面を想定して変数(のようなもの)の使い方を紹介します

パターン① 1行のテーブルをcross join

with const as (
select
 timestamp('2018-1-1') as date
)

select
  *
from
  table,const
where
  _PARTITIONTIME = date

パターン② ユーザー定義関数を使う

create temporary function getDate() as (TIMESTAMP('2018-1-1'));

select
  *
from
  table
where
  _PARTITIONTIME = getDate()
15
5
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
15
5