[英]BigQuery: Concatenating arrays with window clause
I am working with data from tables for which dummy data could be:我正在处理表中的数据,其虚拟数据可能是:
WITH
Sequences AS (
SELECT [0, 1, 3] AS some_numbers UNION ALL
SELECT [2, 4, 8] UNION ALL
SELECT [0, 5] UNION ALL
SELECT [2, 16] UNION ALL
SELECT [0, 7]
),
SELECT
some_numbers[ORDINAL(1)] AS grp,
some_numbers[ORDINAL(2)] AS sub_grp,
some_numbers
FROM Sequences
I want to combine the arrays within groups of grp
but only till last 1 sub_grp
.我想将 arrays 组合到
grp
组中,但只能到最后 1 个sub_grp
。
I tried small things like:我尝试了一些小事情,比如:
SELECT
grp,
sub_grp,
ARRAY_CONCAT_AGG(some_numbers) OVER (PARTITION BY grp ORDER BY sub_grp ROWS 1 PRECEDING)
FROM numbers
However this results in error:然而,这会导致错误:
Analytic function ARRAY_CONCAT_AGG is not supported.
解析 function 不支持ARRAY_CONCAT_AGG。
Any pointer how can I fix this problem?任何指针我该如何解决这个问题?
EDIT Adding the expected output. I am expecting the output to be:编辑添加预期的 output。我希望 output 是:
But regardless as I am hit by a hard wall of error;
但无论如何,当我被错误的硬墙击中时; any hint which let me give working example, will be of immense help.
让我给出工作示例的任何提示都将提供巨大的帮助。 I can adapt it to my use case:)
我可以根据我的用例调整它:)
Hope below will unblock your efforts希望下面能解除你的努力
select * except(arrs, pos),
format('%t', (select array_concat_agg(arr) from t.arrs)) grouped_numbers
from (
select grp, sub_grp,
array_agg(struct(some_numbers as arr)) over win arrs,
row_number() over(partition by grp order by sub_grp) pos
from sequences, unnest([struct(some_numbers[ordinal(1)] as grp, some_numbers[ordinal(2)] as sub_grp)])
window win as (partition by grp order by sub_grp rows between current row and 1 following)
) t
where mod(pos, 2) = 1
if applied to sample data in your question - output is如果应用于您问题中的样本数据 - output 是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.