BigQuery 查询对象 ID 相同的所有数组的 returns 个不同的数组并集

BigQuery query which returns distinct array union of all arrays where the object ID is the same

我想查询 BigQuery,其中返回两列,一个不同的对象 ID 列,以及基于对象 ID 相同的数组值的不同数组值组合。

例如,假设我在 BigQuery 中有这个 table:

+-----------+-----------+
| object_id |  array    |
+-----------+-----------+
| a         | 1,2,3,4,5 |
| a         | 2,3,4,5,6 |
| b         | 9,8,7,6   |
+-----------+-----------+

我想运行查询returns以下数据:

+-----------+-------------+
| object_id |  array      |
+-----------+-------------+
| a         | 1,2,3,4,5,6 |
| b         | 9,8,7,6     |
+-----------+-------------+

我认为查询应该与相似,但我不太清楚如何使用数组元素的不同组合来获取不同的对象 ID。

使用array_concat_agg:

WITH `project.dataset.table` AS (
  SELECT 'a' id, [1,2,3,4,5] arr UNION ALL
  SELECT 'a', [2,3,4,5,6] UNION ALL
  SELECT 'b', [9,8,7,6]
)
SELECT
    id,
    ARRAY(SELECT DISTINCT x FROM UNNEST(concatenated) as x) as a
FROM (
  SELECT
    id,
    ARRAY_CONCAT_AGG(arr) as concatenated
  FROM `project.dataset.table`
  GROUP BY id
)

考虑以下[苗条]解决方案

select id, array_agg(distinct el) as arr
from `project.dataset.table`,
unnest(arr) el
group by id