在内部(嵌套)行 ID 上加入数组
Joining arrays on internal(nested ) row ids
如何在连接两个表时join/sum逐行增加数组值(即表示数组中偏移量的行)
即下面给出两个表
row_id = 1
的输出应该是 STRUCT("A",ARRAY[41,43,45])
即每个 internal_id
对应于每个数组
中的索引的 col1.val1
和 col2.val2
值之和的数组
WITH table1 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[1,2,3] as val1) AS col1
),
table2 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[40,41,42] as val2) AS col2
)
编辑:
所以我尝试执行以下操作,首先取消嵌套数组,但结果不正确,即将结果数组放在每个索引处,即有 9 行而不是 3
WITH table1 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[1,2,3] as val1) AS col1
),
table2 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[40,41,42] as val2) AS col2
),
table1_unnested as(
select row_id, col1.internal_id, val1 from table1, unnest(col1.val1) as val1
),
table2_unnested as(
select row_id, col2.internal_id, val2 from table2, unnest(col2.val2) as val2
)
select t1.row_id, t1.internal_id, ARRAY_AGG(t1.val1+t2.val2) as newval
from table1_unnested as t1
join table2_unnested as t2 using(row_id)
group by t1.row_id, t1.internal_id
你非常接近 - 请参阅下面的更正
WITH table1 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[1,2,3] as val1) AS col1
),
table2 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[40,41,42] as val2) AS col2
),
table1_unnested as(
select row_id, col1.internal_id, val1, offset from table1, unnest(col1.val1) as val1 with offset
),
table2_unnested as(
select row_id, col2.internal_id, val2, offset from table2, unnest(col2.val2) as val2 with offset
)
select t1.row_id, t1.internal_id, ARRAY_AGG(t1.val1+t2.val2) as newval
from table1_unnested as t1
join table2_unnested as t2 using(row_id, offset)
group by t1.row_id, t1.internal_id
有输出
如您所见 - 我在五 (5) 个位置添加了 offset
来修复您的原始查询
如何在连接两个表时join/sum逐行增加数组值(即表示数组中偏移量的行)
即下面给出两个表
row_id = 1
的输出应该是 STRUCT("A",ARRAY[41,43,45])
即每个 internal_id
对应于每个数组
col1.val1
和 col2.val2
值之和的数组
WITH table1 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[1,2,3] as val1) AS col1
),
table2 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[40,41,42] as val2) AS col2
)
编辑: 所以我尝试执行以下操作,首先取消嵌套数组,但结果不正确,即将结果数组放在每个索引处,即有 9 行而不是 3
WITH table1 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[1,2,3] as val1) AS col1
),
table2 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[40,41,42] as val2) AS col2
),
table1_unnested as(
select row_id, col1.internal_id, val1 from table1, unnest(col1.val1) as val1
),
table2_unnested as(
select row_id, col2.internal_id, val2 from table2, unnest(col2.val2) as val2
)
select t1.row_id, t1.internal_id, ARRAY_AGG(t1.val1+t2.val2) as newval
from table1_unnested as t1
join table2_unnested as t2 using(row_id)
group by t1.row_id, t1.internal_id
你非常接近 - 请参阅下面的更正
WITH table1 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[1,2,3] as val1) AS col1
),
table2 AS (
SELECT 1 AS row_id, STRUCT("A" AS internal_id, ARRAY[40,41,42] as val2) AS col2
),
table1_unnested as(
select row_id, col1.internal_id, val1, offset from table1, unnest(col1.val1) as val1 with offset
),
table2_unnested as(
select row_id, col2.internal_id, val2, offset from table2, unnest(col2.val2) as val2 with offset
)
select t1.row_id, t1.internal_id, ARRAY_AGG(t1.val1+t2.val2) as newval
from table1_unnested as t1
join table2_unnested as t2 using(row_id, offset)
group by t1.row_id, t1.internal_id
有输出
如您所见 - 我在五 (5) 个位置添加了 offset
来修复您的原始查询