【问题标题】:Clickhouse: runningAccumulate() does not work as I expectClickhouse:runningAccumulate() 不像我预期的那样工作
【发布时间】:2021-01-29 10:47:33
【问题描述】:

说,我们有一张桌子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 相加)。

【问题讨论】:

    标签: clickhouse


    【解决方案1】:

    对于累积函数,元素的顺序很重要,因此只需添加 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].

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2018-11-19
      • 1970-01-01
      • 2016-03-07
      • 2014-05-16
      • 1970-01-01
      • 2022-01-11
      • 2012-07-13
      • 1970-01-01
      相关资源
      最近更新 更多