假设我们有一个表testint
。
SELECT *
FROM testint
┌─f1─┬─f2─┐
│ 2 │ 3 │
│ 2 │ 3 │
│ 4 │ 5 │
│ 4 │ 5 │
│ 6 │ 7 │
│ 6 │ 7 │
└────┴────┘
我们尝试用sumState()
查询runningAccumulate()
。
SELECT runningAccumulate(col)
FROM
(
SELECT sumState(f1) AS col
FROM testint
GROUP BY f1
)
┌─runningAccumulate(col)─┐
│ 8 │
│ 12 │
│ 24 │
└────────────────────────┘
为什么响应的第一行是8
,而不是4
?如果我们按f1分组,第一行似乎是4
(我们在f1
列中对第一个2
和第二个2
求和)。
对于累加函数,元素的顺序很重要,所以只需添加order BY来修复它:
SELECT runningAccumulate(col)
FROM
(
SELECT sumState(f1) AS col
FROM testint
GROUP BY f1
ORDER BY f1 ASC /* <-- */
)
您得到的结果[8,12,24]输入数据[8,4,12]应该使用有序输入[4,8,12]。