【问题标题】:Decreasing running total in oracle sql减少oracle sql中的运行总数
【发布时间】:2018-04-01 16:33:53
【问题描述】:

表 A

Week    Column_1   Column_2
0       9          92
1       0          84
2       1          84
3       4          83

我想要的结果是

Week Column_1  Column_2  Remaining
0    9         92        83
1    0         84        83
2    1         84        82
3    4         83        78

所以,如果你注意到的话。我想将第 0 周的 Remaining 计算为 Column_2 - Column_1 并且在第 0 周之后我想将 Remaining 计算为 Remaining - Column_1

有可能吗?

【问题讨论】:

标签: oracle oracle-sqldeveloper cumulative-sum


【解决方案1】:

您可以使用此查询。

SELECT a.*,
       SUM (CASE
              WHEN a.week = 0 THEN column_2 - column_1
              ELSE -column_1
            END)
         over (
           ORDER BY week )
FROM   tablea a;  

【讨论】:

    猜你喜欢
    • 2018-04-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2022-01-10
    • 2014-07-04
    • 1970-01-01
    相关资源
    最近更新 更多