AWS Athena中的整数数组总和

时间:2018-10-18 05:47:09

标签: amazon-web-services amazon-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错误和我缺少的东西!!。 任何意见或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

例如,您可以使用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