如何在 BigQuery 中取消透视?

How to unpivot in BigQuery?

不确定调用什么函数,但转置是我能想到的最接近的函数。

我在 BigQuery 中有一个 table 配置如下:

但我想查询一个配置如下的 table:

创建此 table 的 SQL 代码是什么样的?

谢谢!

2020 更新fhoffa.x.unpivot()

参见:

我创建了一个 public 持久性 UDF。如果你有一个 table a,你可以将整行给 UDF 以使其不旋转:

SELECT geo_type, region, transportation_type, unpivotted
FROM `fh-bigquery.public_dump.applemobilitytrends_20200414` a
  , UNNEST(fhoffa.x.unpivot(a, '_2020')) unpivotted

它将 table 转换成这样:

进入这个


如评论所述,我上面的解决方案没有解决问题。

所以这是一个变体,同时我看看如何将所有内容集成为一个:

CREATE TEMP FUNCTION unpivot(x ANY TYPE) AS (
(
  SELECT 
   ARRAY_AGG(STRUCT(
     REGEXP_EXTRACT(y, '[^"]+') AS key
   , REGEXP_EXTRACT(y, ':([0-9]+)') AS value
   ))
  FROM UNNEST((
    SELECT REGEXP_EXTRACT_ALL(json,'"[smlx][meaxl]'||r'[^:]+:\"?[^"]+?') arr
    FROM (SELECT TO_JSON_STRING(x) json))) y
)
);

SELECT location, unpivotted.*
FROM `robotic-charmer-726.bl_test_data.reconfiguring_a_table` x
  , UNNEST(unpivot(x)) unpivotted


上一个回答:

使用 table 的 UNION(在 BigQuery 中使用“,”),加上一些列别名:

SELECT Location, Size, Quantity
FROM (
  SELECT Location, 'Small' as Size, Small as Quantity FROM [table]
), (
  SELECT Location, 'Medium' as Size, Medium as Quantity FROM [table]
), (
  SELECT Location, 'Large' as Size, Large as Quantity FROM [table]
)

@Felipe,我使用标准-sql 进行了尝试,但我在查询的第一行收到错误消息:"Column name Location is ambiguous at [1:8]"

我使用了适合我的备用查询:

SELECT Location, 'Small' as Size, Small as Quantity FROM `table`
UNION ALL
SELECT Location, 'Medium' as Size, Medium as Quantity FROM `table`
UNION ALL
SELECT Location, 'Large' as Size, Large as Quantity FROM `table`

我有一个使用 STRUCTs、ARRAYs 和 CROSS JOIN + UNNEST 的解决方案:

WITH
  My_Table_Metrics_Data AS (
  SELECT
    ...,
    [
        STRUCT('...' AS Metric, ... AS Data),
        STRUCT('...' AS Metric, ... AS Data),
    ] AS Metrics_Data
  FROM
    `My_Dataset.My_Table`
  WHERE
    ...
  )
SELECT
  ...,
  Metric_Data
FROM
  My_Table_Metrics_Data
CROSS JOIN
  UNNEST(My_Table_Metrics_Data.Metrics_Data) AS Metric_Data

完整的解释和说明:https://yuhuisdatascienceblog.blogspot.com/2018/06/how-to-unpivot-table-in-bigquery.html

2021 年更新:

BigQuery 中引入了一个新的 UNPIVOT 运算符。

在使用 UNPIVOT 将 Q1、Q2、Q3、Q4 旋转到销售额和季度列之前:

product Q1 Q2 Q3 Q4
Kale 51 23 45 3
Apple 77 0 25 2

使用 UNPIVOT 将 Q1、Q2、Q3、Q4 轮换为销售额和季度列后:

product sales quarter
Kale 51 Q1
Kale 23 Q2
Kale 45 Q3
Kale 3 Q4
Apple 77 Q1
Apple 0 Q2
Apple 25 Q3
Apple 2 Q4

查询:

WITH Produce AS (
  SELECT 'Kale' as product, 51 as Q1, 23 as Q2, 45 as Q3, 3 as Q4 UNION ALL
  SELECT 'Apple', 77, 0, 25, 2
)
SELECT * FROM Produce
UNPIVOT(sales FOR quarter IN (Q1, Q2, Q3, Q4))