0
0

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】IGNORE NULLSについて(NULLが邪魔な時に無視できる機能)

Last updated at Posted at 2022-12-22

今回の課題

FIRST_VALUEなどのナビゲーション関数を使用する時や、
array_aggで配列を生成する際に、NULLを除いて処理を実行したい。

そういった時に、IGNORE NULLSというオプションが役に立つ。

使用クエリの例

今回は、array_aggの例でメモをする。

select
    enquete_id
    , array_to_string(array_agg(case when questions_sort_code = 0 then answer end ignore nulls order by rows_sort_code), '|') as answer
from
    `テーブル名`
group by
    enquete_id

クエリの解説

1)answerカラムのデータを配列で結合して一つのレコードに格納

array_agg(case when questions_sort_code = 0 then answer end ignore nulls order by rows_sort_code)

enquete_id毎に、question_sort_codeが0のデータのNULLを除外して、
answerrows_sort_codeの昇順で、配列にして1つのレコードに格納する。

2)1つのレコードに配列で格納されているものを一つの文字列にする

array_to_string(array_agg(case when questions_sort_code = 0 then answer end ignore nulls order by rows_sort_code), '|')

1)で配列にして格納したデータを、|を間に入れて、文字列で結合してレコードに格納する。

※参考:ナビゲーション関数(IGNORE NULLSについて記載)

以上です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?