Share via


UNPIVOT 子句

適用於:檢查標示為是 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 NULLSEXCLUDE NULLS

    是否要在 中篩選出具有 NULL 的數據 value_column列。 預設值為 EXCLUDE NULLS

  • value_column

    不合格的數據行別名。 此數據行會保存值。 ech value_column 的類型是對應 column_name 數據行類型的最不通用類型。

  • unpivot_column

    不合格的數據行別名。 此資料行會保存旋轉 column_name的 s 或其 column_alias名稱。 unpivot_column 的類型是 STRING

    如果是多重值UNPIVOT,如果沒有任何 column_alias,則值會是分隔 column_names 的'_'串連。

  • column_name

    識別將取消樞紐的關聯數據行。 名稱可能限定。 所有 column_name都必須共用最不通用的類型。

  • column_alias

    unpivot_column所使用的選擇性名稱。

  • table_alias

    選擇性地指定結果數據表的標籤。 table_alias如果 包含 column_identifier,其數目必須符合 所產生的UNPIVOT數據行數目。

結果

下列表單的臨時表:

  • table_reference除了名為 column_names 的數據行以外的所有數據行。
  • unpivot_column 型別的 STRING
  • value_column其比column_name對之最不常見的型別的 。

範例

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