設定上人外122歳(♀)の精神安穏日記

広告の消し方 https://masavo.jp/ad

【Redshift】'1,2,3'のようなカンマ区切りの文字列をArrayにし平たく(unnest)してwhere inに利用する方法

公式document

クエリのネストを解除するために、Amazon Redshift には、SUPER 配列を反復処理する 2 つの方法があります。FROM 句の PartiQL 構文と UNNEST オペレーションです。

docs.aws.amazon.com

SQLサンプル

WITH
  input_data as (
    SELECT split_to_array('1,2,3', ',') as ids
  )
-- , ids as (
  SELECT 
    id
  FROM 
    input_data, 
    input_data.ids AS id
-- )
-- select * from users where in (select id from ids);

活用どころ

文字列で'1,2,3'を渡すときに便利(tableauなど)

参考サイト

stackoverflow.com