BigQuery 数组元素乘法
BigQuery Array Element Multiplicaton
菜鸟问题在这里:
我有一个 BQ table,其嵌套结构包含 2 个数组,x 和 y。我想创建一个新的 xy 数组或取消嵌套 2 个数组并创建 x、y 和 xy 值的扁平 table。
WITH
tests AS (
SELECT
[ struct("test1" AS test,
[0.1576,0.9706,0.9572,0.4854,0.8003,0.1419,0.4218,0.9157,0.7922,0.9595] AS x,
[11,12,13,14,15,16,17,18,19,20] AS y),
struct("test2" AS test,
[0.8147,0.9058,0.1270,0.9134,0.6324,0.0975,0.2785,0.5469,0.9575, 0.9649] AS x,
[11,12,13,14,15,16,17,18,19,20] AS y),
struct("test3" AS test,
[0.6557,0.0357,0.8491,0.9340,0.6787,0.7577,0.7431,0.3922,0.6555,0.1712] AS x,
[11,12,13,14,15,16,17,18,19,20] AS y)
] AS measurements)
SELECT
test, x,y
FROM
tests t, unnest(t.measurements)
我想扩展上面的查询以获得一个扁平化的 table 测试编号,x,y 和 x*y 像这样,或类似的东西:
tests
x
y
x*y
test1
0.1576
11
1.7366
test1
0.9706
12
11.6472
test1
0.9572
13
12.4436
test1
0.4854
14
6.7956
...
...
...
...
test3
0.1712
20
3.4240
考虑以下方法
select test, x, y, round(x*y, 4) as x_multiply_y
from tests, unnest(measurements),
unnest(x) x with offset
join unnest(y) y with offset
using(offset)
如果应用于您问题中的示例数据 - 输出为
菜鸟问题在这里:
我有一个 BQ table,其嵌套结构包含 2 个数组,x 和 y。我想创建一个新的 xy 数组或取消嵌套 2 个数组并创建 x、y 和 xy 值的扁平 table。
WITH
tests AS (
SELECT
[ struct("test1" AS test,
[0.1576,0.9706,0.9572,0.4854,0.8003,0.1419,0.4218,0.9157,0.7922,0.9595] AS x,
[11,12,13,14,15,16,17,18,19,20] AS y),
struct("test2" AS test,
[0.8147,0.9058,0.1270,0.9134,0.6324,0.0975,0.2785,0.5469,0.9575, 0.9649] AS x,
[11,12,13,14,15,16,17,18,19,20] AS y),
struct("test3" AS test,
[0.6557,0.0357,0.8491,0.9340,0.6787,0.7577,0.7431,0.3922,0.6555,0.1712] AS x,
[11,12,13,14,15,16,17,18,19,20] AS y)
] AS measurements)
SELECT
test, x,y
FROM
tests t, unnest(t.measurements)
我想扩展上面的查询以获得一个扁平化的 table 测试编号,x,y 和 x*y 像这样,或类似的东西:
tests | x | y | x*y |
---|---|---|---|
test1 | 0.1576 | 11 | 1.7366 |
test1 | 0.9706 | 12 | 11.6472 |
test1 | 0.9572 | 13 | 12.4436 |
test1 | 0.4854 | 14 | 6.7956 |
... | ... | ... | ... |
test3 | 0.1712 | 20 | 3.4240 |
考虑以下方法
select test, x, y, round(x*y, 4) as x_multiply_y
from tests, unnest(measurements),
unnest(x) x with offset
join unnest(y) y with offset
using(offset)
如果应用于您问题中的示例数据 - 输出为