AWS Athena 中整数数组的总和
Sum of integer array in AWS Athena
我有一列包含整数数组,如下所示
{
"X":[-11,-11,-11,-17],
"Y":[184,180,184,184],
"Z":[144,140,144,142]
}
我将 X、Y 和 Z 映射为列,但我没有通过使用 sum
得到总和
我试过了
SELECT X AS items,
x_item AS (
SELECT i AS array_items
FROM "test_database"."quicktest", UNNEST(items) AS t(i)
)
SELECT array_items, sum(val) AS total
&
select sum(X) FROM "test_database"."quicktest"
它给了我 sql 错误和我遗漏的东西!!
如有任何意见或建议,我们将不胜感激。
例如你可以使用reduce函数:
https://prestodb.io/docs/0.172/functions/array.html
https://prestodb.io/docs/0.172/functions/lambda.html#reduce
示例代码:
SELECT x, reduce(x, 0, (s, x) -> s + x, s -> s) as sum_x,
y, reduce(y, 0, (s, x) -> s + x, s -> s) as sum_y,
z, reduce(z, 0, (s, x) -> s + x, s -> s) as sum_z
FROM example
我有一列包含整数数组,如下所示
{
"X":[-11,-11,-11,-17],
"Y":[184,180,184,184],
"Z":[144,140,144,142]
}
我将 X、Y 和 Z 映射为列,但我没有通过使用 sum
得到总和我试过了
SELECT X AS items,
x_item AS (
SELECT i AS array_items
FROM "test_database"."quicktest", UNNEST(items) AS t(i)
)
SELECT array_items, sum(val) AS total
&
select sum(X) FROM "test_database"."quicktest"
它给了我 sql 错误和我遗漏的东西!! 如有任何意见或建议,我们将不胜感激。
例如你可以使用reduce函数:
https://prestodb.io/docs/0.172/functions/array.html
https://prestodb.io/docs/0.172/functions/lambda.html#reduce
示例代码:
SELECT x, reduce(x, 0, (s, x) -> s + x, s -> s) as sum_x,
y, reduce(y, 0, (s, x) -> s + x, s -> s) as sum_y,
z, reduce(z, 0, (s, x) -> s + x, s -> s) as sum_z
FROM example