UNPIVOT 句

適用対象:check marked yes Databricks SQL 「はい」のチェック マーク Databricks Runtime 12.2 LTS 以降。

列のグループを行に回転し、一覧表示された列を折りたたむことで 、table_reference の行を変換します。最初の新しい列は、元の列グループ名 (または、その別名) を値として保持します。この列の後に、各列グループの値を含む列のグループが続きます。

構文

table_reference UNPIVOT [ { INCLUDE NULLS | EXCLUDE NULLS } ]
  { single_value | multi_value }
  ( value_column
    FOR unpivot_column IN ( { column_name [ column_alias ] } [, ...] ) )
  [ table_alias ]

single_value
  ( value_column
    FOR unpivot_column IN ( { column_name [ column_alias ] } [, ...] ) )

multi_value
  ( ( value_column [, ...] )
    FOR unpivot_column IN ( { ( column_name [, ...] ) [ column_alias ] } [, ...] ) )

パラメーター

  • table_reference

    UNPIVOT 操作のサブジェクトを識別します。

  • INCLUDE NULLS または EXCLUDE NULLS

    value_columnNULL で行をフィルター処理するかどうか。 既定では、 EXCLUDE NULLSです。

  • value_column

    非修飾列の別名。 この列には値が保持されます。 各 value_column の型は、対応する column_name 列型の最小共通型になります。

  • unpivot_column

    非修飾列の別名。 この列には、回転した column_name またはそれらの column_alias の名前が保持されます。 unpivot_column の型は STRING です。

    複数値の UNPIVOT の場合、column_alias がない場合に、値は '_' で区切られた column_name の連結になります。

  • column_name

    ピボット解除されるものに関連する列を識別します。 名前は修飾されている場合があります。 column_name はすべて、最小共通型を共有する必要があります。

  • column_alias

    unpivot_column で使用される省略可能な名前。

  • table_alias

    必要に応じて、結果のテーブルのラベルを指定します。 table_aliascolumn_identifier が含まれる場合、それらの数は UNPIVOT によって生成される列の数と一致する必要があります。

結果

次の形式の一時テーブルとなります。

  • column_name という名前が付けられた列を除く、table_reference のすべての列。
  • STRING 型の unpivot_column
  • それらの一致する column_name の最少共通型の value_column

- A single column UNPIVOT
> CREATE OR REPLACE TEMPORARY VIEW sales(location, year, q1, q2, q3, q4) AS
  VALUES ('Toronto'      , 2020, 100 , 80 , 70, 150),
         ('San Francisco', 2020, NULL, 20 , 50,  60),
         ('Toronto'      , 2021, 110 , 90 , 80, 170),
         ('San Francisco', 2021, 70  , 120, 85, 105);

> SELECT *
    FROM sales UNPIVOT INCLUDE NULLS
    (sales FOR quarter IN (q1       AS `Jan-Mar`,
                           q2       AS `Apr-Jun`,
                           q3       AS `Jul-Sep`,
                           sales.q4 AS `Oct-Dec`));
 location      year quarter  sales
 —------------ —--- —------ —-----
 Toronto       2020 Jan-Mar    100
 Toronto       2020 Apr-Jun     80
 Toronto       2020 Jul-Sep     70
 Toronto       2020 Oct-Dec    150
 San Francisco 2020 Jan-Mar   null
 San Francisco 2020 Apr-Jun     20
 San Francisco 2020 Jul-Sep     50
 San Francisco 2020 Oct-Dec     60
 Toronto       2021 Jan-Mar    110
 Toronto       2021 Apr-Jun     90
 Toronto       2021 Jul-Sep     80
 Toronto       2021 Oct-Dec    170
 San Francisco 2021 Jan-Mar     70
 San Francisco 2021 Apr-Jun    120
 San Francisco 2021 Jul-Sep     85
 San Francisco 2021 Oct-Dec    105

-- This is equivalent to:
> SELECT location, year,
         inline(arrays_zip(array('Jan-Mar', 'Apr-Jun', 'Jul-Sep', 'Oct-Dec'),
                           array(q1       , q2       , q3       , q4)))
         AS (quarter, sales)
    FROM sales;

- A multi column UNPIVOT
> CREATE OR REPLACE TEMPORARY VIEW oncall
         (year, week, area      , name1   , email1              , phone1     , name2   , email2              , phone2) AS
  VALUES (2022, 1   , 'frontend', 'Freddy', 'fred@alwaysup.org' , 15551234567, 'Fanny' , 'fanny@lwaysup.org' , 15552345678),
         (2022, 1   , 'backend' , 'Boris' , 'boris@alwaysup.org', 15553456789, 'Boomer', 'boomer@lwaysup.org', 15554567890),
         (2022, 2   , 'frontend', 'Franky', 'frank@lwaysup.org' , 15555678901, 'Fin'   , 'fin@alwaysup.org'  , 15556789012),
         (2022, 2   , 'backend' , 'Bonny' , 'bonny@alwaysup.org', 15557890123, 'Bea'   , 'bea@alwaysup.org'  , 15558901234);

> SELECT *
    FROM oncall UNPIVOT ((name, email, phone) FOR precedence IN ((name1, email1, phone1) AS primary,
                                                                 (name2, email2, phone2) AS secondary));
 year week area     precedence name   email              phone
 ---- ---- -------- ---------- ------ ------------------ -----------
 2022    1 frontend primary    Freddy fred@alwaysup.org  15551234567
 2022    1 frontend secondary  Fanny  fanny@lwaysup.org  15552345678
 2022    1 backend  primary    Boris  boris@alwaysup.org 15553456789
 2022    1 backend  secondary  Boomer boomer@lwaysup.org 15554567890
 2022    2 frontend primary    Franky frank@lwaysup.org  15555678901
 2022    2 frontend secondary  Fin    fin@alwaysup.org   15556789012
 2022    2 backend  primary    Bonny  bonny@alwaysup.org 15557890123
 2022    2 backend  secondary  Bea    bea@alwaysup.org   15558901234

-- This is equivalent to:
> SELECT year, week, area,
         inline(arrays_zip(array('primary', 'secondary'),
                           array(name1, name2),
                           array(email1, email2),
                           array(phone1, phone2)))
         AS (precedence, name, email, phone)
    FROM oncall;