4
5

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.

BigQuery — WITHはネストできる ( サブクエリの中に書ける )

Last updated at Posted at 2018-10-04

書いてみたら出来た。

「WITH はサブクエリの変種なので、クエリ本体に書けるものなら何でも書けるんじゃないか」と理解した。「サブクエリ」というくらいだし。

改めて考えてみれば当たり前のような気もした。

クエリ例

WITH result AS (
   WITH example AS ( SELECT * FROM `dataset.table` )
   SELECT * FROM example
)

SELECT * FROM result

Public Dataset での例

WITH result AS (
   WITH games_wide_limitted AS ( SELECT * FROM `bigquery-public-data.baseball.games_wide` LIMIT 100 )
   SELECT * FROM games_wide_limitted
)

SELECT * FROM result

深いネストの例

WITH result AS (
   WITH reg_season AS (
     WITH  year_2016 AS (
       SELECT * FROM `bigquery-public-data.baseball.games_wide` WHERE year = 2016
     )
     SELECT * FROM year_2016 WHERE seasonType = 'REG'
   )
   SELECT * FROM reg_season
)

SELECT * FROM result

Links

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

4
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?