我正在尝试在逐个列分组之后逐个元素添加数组列。
具有下表A:
id units
1 [1,1,1]
2 [3,0,0]
1 [5,3,7]
3 [2,5,2]
2 [3,2,6]
我想查询以下内容:
select id, sum(units) from A group by id
并获得以下结果:
id units
1 [6,4,8]
2 [6,2,6]
3 [2,5,2]
具有相同id的行中的单位数组在逐个元素处添加。
答案 0 :(得分:2)
尝试此查询:
SELECT id, sumForEach(units) units
FROM (
/* emulate dataset */
SELECT data.1 id, data.2 units
FROM (
SELECT arrayJoin([(1, [1,1,1]), (2, [3,0,0]), (1, [5,3,7]), (3, [2,5,2]), (2, [3,2,6])]) data))
GROUP BY id
/* Result
┌─id─┬─units───┐
│ 1 │ [6,4,8] │
│ 2 │ [6,2,6] │
│ 3 │ [2,5,2] │
└────┴─────────┘
*/