【问题标题】:Updating cumulative sum by two columns [duplicate]按两列更新累积总和[重复]
【发布时间】:2018-03-02 22:10:41
【问题描述】:

大家好,我需要帮助!

我有一个这样的表 sql:

id | in | out
1  | 32 | 23
2  | 4  | 0
3  | 10 | 3

我需要这样的结果:

id | in | out| cumulative
1  | 32 | 23 | 9
2  | 4  | 0  | 13
3  | 10 | 3  | 20

可以在 sql 中做吗?如何?谢谢

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    可以使用这样的子查询:

    select id,
          [in],
          [out],
          (SELECT ABS(SUM([out] - [in])) 
            from ##temp as c2 where id <= c1.id) as cumulative
    from ##temp as c1
    

    ##temp 是你的桌子

    【讨论】:

    • 非常漂亮和KISS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多