除非使用 SELECT AS STRUCT 构建 STRUCT 值,否则标量子查询不能有超过一列

Scalar subquery cannot have more than one column unless using SELECT AS STRUCT to build STRUCT values

我正在尝试在 BigQuery 中编写一个查询,select 一些行基于行号并对列求和。 详细地说,我有两个表:在 table1 中,每个 id 都有我想要求和的 table2 的行数。下面是两个表格的示例。

table1

table2

期望的输出是:

id points1 points2 points3
a 86 99 31
b 91 59 15
c 122 183 118

我创建了一个 UDF,它采用 'neighbors'、n1n2n3 并对 table2 的行求和 row_num在 n1n2n3 中;然后我在下面的查询中回忆起 UDF。

create temp function sum_points(neighbors array<int>) 
returns array<int>
as (sum((select * from `project.dataset.table2` where row_num in unnest(neighbors))));

with cte as (
  select id, array_agg(nn) as neighbors
  from `project.dataset.table1`, unnest([n1, n2, n3]) nn
  group by id
)
select id, sum_points(neighbors) from cte

但是,我收到以下错误:

Scalar subquery cannot have more than one column unless using SELECT AS STRUCT to build STRUCT values; failed to parse CREATE [TEMP] FUNCTION statement at [5:9]

我不太清楚那是什么意思。我试图用 select struct<array<int>> 替换 with 语句中的 select 但它没有用。

更好的选择是连接表并进行聚合。

您可以根据 table2 中的 row_numtable1 中的 n1n2n3 来联接表,如下所示。

SELECT
  id,
  SUM(points1) AS points1,
  SUM(points2) AS points2,
  SUM(points3) AS points3
FROM table1 JOIN table2
ON row_num in (n1, n2, n3)
GROUP BY id

查询输出:

id points1 points2 points3
a 86 99 31
c 122 183 118
b 91 59 15