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

问题描述

比如说,我们有一张桌子 testint

        SELECT *
    FROM testint
    
    ┌─f1─┬─f2─┐
    │  2 │  3 │
    │  2 │  3 │
    │  4 │  5 │
    │  4 │  5 │
    │  6 │  7 │
    │  6 │  7 │
    └────┴────┘

我们尝试使用 runningAccumulate() 查询 sumState()

    SELECT runningAccumulate(col)
    FROM 
    (
        SELECT sumState(f1) AS col
        FROM testint
        GROUP BY f1
    )
    
    ┌─runningAccumulate(col)─┐
    │                      8 │
    │                     12 │
    │                     24 │
    └────────────────────────┘

为什么响应中的第一行是 8,而不是 4?如果我们按 f1 分组,第一行似乎是 4(我们对 2 列中的第一个 2 和第二个 f1 求和)。

解决方法

对于累加函数,元素的顺序很重要,所以只需添加 ORDER BY 来修复它:

SELECT runningAccumulate(col)
FROM 
(
    SELECT sumState(f1) AS col
    FROM testint
    GROUP BY f1
    ORDER BY f1 ASC /* <-- */
)

You got the result [8,12,24] for input data [8,4,12] 什么时候应该使用有序输入 - [ 4,8,12]