简体   繁体   English

BigQuery SQL中的数组列和聚合:为什么值不是全部聚合的?

[英]Array column and aggregation in BigQuery SQL: Why the values are not all aggregated?

I've executed the code below in BigQuery 我在BigQuery中执行了下面的代码

SELECT ( --inner query
         SELECT STRING_AGG(c) FROM t1.array_column c
       ) 
FROM (
        select 1 as f1, ['1','2','3'] as array_column
        union all
        select 2 as f1, ['5','6','7'] as array_column
) t1;

I expected something like 我期待类似的东西

Row|f0_
1  | 1,2,3,4,5,6,7

because there is no GROUP BY in the inner query. 因为内部查询中没有GROUP BY So, I'm expecting STRING_AGG to be evaluated on all the lines. 所以,我期待在所有线路上评估STRING_AGG

SELECT STRING_AGG(c) FROM t1.array_column c

Instead I'm getting something like this: 相反,我得到这样的东西:

Row|f0_
1  |1,2,3
2  |5,6,7

I'm having troubles understand why I have this result 我有麻烦理解为什么我有这个结果

Below is for BigQuery Standard SQL 以下是BigQuery Standard SQL

#standardSQL
SELECT STRING_AGG((SELECT STRING_AGG(c) FROM t1.array_column c)) 
FROM (
  SELECT 1 AS f1, ['1','2','3'] AS array_column UNION ALL
  SELECT 2 AS f1, ['5','6','7'] AS array_column
) t1

and produces 并生产

Row f0_  
1   1,2,3,5,6,7    

Note 1: you were almost there - you were just missing extra STRING_AGG that does final grouping of strings created off of respective array in each row 注1:你几乎就在那里 - 你只是缺少额外的STRING_AGG ,它会对每行中各自数组创建的字符串进行最后的分组

Note 2: because array_column is of ARRAY type it is treated as inner table referenced as t1.array_column as as such - FROM t1.array_column c is equivalent to FROM UNNEST(array_column) c - very cool hidden feature :o) 注意2:因为array_column属于ARRAY类型,所以它被视为内部表,引用为t1.array_column ,因为 - FROM t1.array_column c相当于FROM UNNEST(array_column) c - 非常酷的hidden功能:o)

This is your query: 这是您的查询:

SELECT (SELECT STRING_AGG(c) FROM t1.array_column c
       ) 
FROM (select 1 as f1, ['1', '2', '3'] as array_column
      union all
      select 2 as f1, ['5', '6', '7'] as array_column
     ) t1;

First, I'm surprised it works. 首先,我很惊讶它有效。 I thought you needed unnest() : 我以为你需要unnest()

SELECT (SELECT STRING_AGG(c) FROM UNNEST(t1.array_column) c
       ) 

What is happening? 怎么了? Well, this would be more obvious if you selected f1 . 好吧,如果你选择f1 ,这将更加明显。 Then you would get: 然后你会得到:

1     1,2,3
2     5,6,7

This should make it more clear. 这应该更清楚。 For each row in t1 (and there are two rows), your code is: 对于t1每一行(并且有两行),您的代码是:

  • unnesting the array into rows with a column called c . 使用名为c的列将数组排除在行之外。
  • reaggregating those rows into a string (with no name) 将这些行重新聚合成一个字符串(没有名称)

If you want to combine the elements in the arrays, use array_concat_agg() : 如果要组合数组中的元素,请使用array_concat_agg()

SELECT array_concat_agg(array_column)
FROM (select 1 as f1, ['1','2','3'] as array_column
      union all
      select 2 as f1, ['5','6','7'] as array_column
     ) t1;

If you want this represented as a string instead of an array, use array_to_string() : 如果您希望将其表示为字符串而不是数组,请使用array_to_string()

SELECT array_to_string(array_concat_agg(array_column), ',')
FROM (select 1 as f1, ['1','2','3'] as array_column
      union all
      select 2 as f1, ['5','6','7'] as array_column
     ) t1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM