Clickhouse:runningAccumulate() 没有像我预期的那样工作

Clickhouse: runningAccumulate() does not work as I expect

说,我们有一个 table 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,4,12]得到结果[8,12,24]有序输入 - [4, 8, 12].