[英]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
每一行(并且有两行),您的代码是:
c
. c
的列将数组排除在行之外。 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.