聚合重复字段的唯一值

aggregate unique values of repeated field

我的 table 中有一个重复字段。我想 运行 聚合查询并接收与我的查询匹配的唯一值数组。我已经尝试了此查询的几种不同变体:

with t as (select * from unnest([
    STRUCT("foo" as name, ["red", "blue"] as color)
  , STRUCT("foo", ["blue"])
  , STRUCT("foo", NULL)
  , STRUCT("foo", ["green"])
  , STRUCT("bar", ["orange", "black"])
  , STRUCT("bar", ["black", "white"])
]))
select
    (select color from unnest(array_concat_agg(color))) as color
from t
group by name

期望的结果是:

name  | color
=====================================
foo   | ["red", "blue", "green"]
bar   | ["orange", "black", "white"]

这个特定的查询给出了 Aggregate function ARRAY_CONCAT_AGG not allowed in UNNEST at [10:31],但我没有在文档中找到这个错误,而且我找不到一个直观的原因说明为什么会有这样的限制,也找不到我如何解决这个错误。

我正在做的事情是否本质上需要额外级别的嵌套查询?

这会得到你想要的结果:

select t.name, array_agg(distinct color)
from (select name, array_concat_agg( color) as colors
      from t
      group by name
     ) t cross join
     unnest(colors) color
group by t.name;

您的查询有几个问题,特别是带有 unnest() 的子查询将 return 多行。

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT name, ARRAY_AGG(DISTINCT color) color
FROM `project.dataset.your_table`, UNNEST(color) color
GROUP BY name

您可以使用您问题中的虚拟数据来测试和玩上面的

#standardSQL
WITH `project.dataset.your_table` AS (
  SELECT * FROM UNNEST([
    STRUCT("foo" AS name, ["red", "blue"] AS color)
  , STRUCT("foo", ["blue"])
  , STRUCT("foo", NULL)
  , STRUCT("foo", ["green"])
  , STRUCT("bar", ["orange", "black"])
  , STRUCT("bar", ["black", "white"])
]))
SELECT name, ARRAY_AGG(DISTINCT color) color
FROM `project.dataset.your_table`, UNNEST(color) color
GROUP BY name

结果为

Row name    color    
1   bar     orange   
            white    
            black    
2   foo     red  
            blue     
            green