在 bigquery 中聚合列并构建数组
Posted
技术标签:
【中文标题】在 bigquery 中聚合列并构建数组【英文标题】:Aggregate columns and build array in bigquery 【发布时间】:2020-07-28 20:07:12 【问题描述】:从这样的表开始
WITH table AS (
SELECT 1001 as ID, 1 As Color_blue, 0 AS Color_red, 0 AS Color_black UNION ALL
SELECT 1002 as ID, 0 As Color_blue, 0 AS Color_red, 1 AS Color_black UNION ALL
SELECT 1003 as ID, 0 As Color_blue, 1 AS Color_red, 0 AS Color_black UNION ALL
SELECT 1004 as ID, 0 As Color_blue, 0 AS Color_red, 1 AS Color_black )
SELECT *
FROM table
我想聚合所有特征列并构建一个数组,如下所示:
select ID, array<float64>[Color_blue, Color_red, Color_black] features
from table
但是,我想动态地包含所有列名称,而无需在查询中对它们进行硬编码。输出应该保持不变。 假设我不知道列名(只有“ID”),如何创建具有所有功能的浮点数组?
【问题讨论】:
【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
SELECT id,
ARRAY(
SELECT CAST(val AS FLOAT64)
FROM UNNEST(SPLIT(TRIM(FORMAT('%t', t), '()'))) val
WHERE CAST(id AS STRING) != val
) AS features
FROM `project.dataset.table` t
当应用于您问题的样本数据时 - 结果是
Row id features
1 1001 1.0
0.0
0.0
2 1002 0.0
0.0
1.0
3 1003 0.0
1.0
0.0
4 1004 0.0
0.0
1.0
【讨论】:
以上是关于在 bigquery 中聚合列并构建数组的主要内容,如果未能解决你的问题,请参考以下文章