将BigQuery中的数组与空数组连接

时间:2018-12-10 22:41:28

标签: google-bigquery standard-sql

我有一个看起来像这样的表:

enter image description here

然后,我尝试将每种标签类型的label1,label2,label3聚合到一个数组中,然后,最后,我想将所有非null标签放入一个组合数组中。所以我的查询看起来像这样

#standardSQL
WITH
table AS (
SELECT 'abc' id, 1 label1, 12 label2, 122 label3 UNION ALL
SELECT 'abc', 1, 12, 129 UNION ALL
SELECT 'xyz', 2, 23, NULL UNION ALL
SELECT 'xyz', 2, 24, NULL
),

each_label_agg AS (
 SELECT
 id,
 ARRAY_AGG(label1 IGNORE NULLS) AS label1_agg,
 ARRAY_AGG(label2 IGNORE NULLS) AS label2_agg,
 ARRAY_AGG(label3 IGNORE NULLS) AS label3_agg
FROM
table
GROUP BY
 id)
SELECT
 each_label_agg.*,
 ARRAY_CONCAT(each_label_agg.label1_agg, each_label_agg.label2_agg, 
 each_label_agg.label3_agg) AS combined_labels
FROM
 each_label_agg

输出看起来像这样:

enter image description here

但是在输出中,我期望ID为combined_labels的{​​{1}}为[2,2,23,24]

xyz参数在ignore nulls中不起作用。我猜测array_concat会由于combined_labels处的空数组而变形。如何获得label3combined_labels的期望xyz

2 个答案:

答案 0 :(得分:2)

#standardSQL
WITH table AS (
  SELECT 'abc' id, 1 label1, 12 label2, 122 label3 UNION ALL
  SELECT 'abc', 1, 12, 129 UNION ALL
  SELECT 'xyz', 2, 23, NULL UNION ALL
  SELECT 'xyz', 2, 24, NULL
), each_label_agg AS (
 SELECT
   id,
   ARRAY_AGG(label1 IGNORE NULLS) AS label1_agg,
   ARRAY_AGG(label2 IGNORE NULLS) AS label2_agg,
   ARRAY_AGG(label3 IGNORE NULLS) AS label3_agg
  FROM table
  GROUP BY id
)
SELECT
  each_label_agg.*,
  ARRAY_CONCAT(
   IFNULL(each_label_agg.label1_agg, []), 
   IFNULL(each_label_agg.label2_agg, []),
   IFNULL(each_label_agg.label3_agg, [])
  ) AS combined_labels
FROM each_label_agg

答案 1 :(得分:1)

此问题的原因是因为BigQuery对NULL的值具有limitations,并且如果{{3}中记载的任何参数为NULL,则数组生成将返回NULL }}。因此,用它们代替空数组很方便(因为NULL和空数组是BigQuery中两个不同的值)