如何在PostgreSQL中根据条件获取运行总和

huangapple go评论142阅读模式
英文:

How to get running sum within condition by postgres

问题

以下是您要翻译的部分:

I have following table

no status amount
1  2      10000
2  3      10000
3  2       1000
4  2     -11000

I got running some by using following query.

SELECT
    main.no
    , main.status
    , main.amount
    , SUM(main.amount) OVER ( 
        ORDER BY
            main.no ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) 
from
    table AS main 

It returned follows. But I would like to improve my formula by refering to status code.

no status amount running_sum
1  2      10000  10000
2  3      10000  20000
3  2       1000  21000
4  2     -11000  10000

My desired result is following.

I would like to get running sum only in status = 2 after another status code.

no status amount running_sum
1  2      10000  10000
2  3      10000  20000
3  2       1000  11000
4  2     -11000  0

How to add conditioning in my formula?

Are there any good way to achieve this?

Thanks

英文:

I have following table

no status amount 
1  2      10000 
2  3      10000
3  2       1000
4  2     -11000

I got running some by using following query.

SELECT
    main.no
    , main.status
    , main.amount
    , SUM(main.amount) OVER ( 
        ORDER BY
            main.no ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) 
from
    table AS main 

It returned follows. But I would like to improve my formula by refering to status code.

no status amount running_sum
1  2      10000  10000
2  3      10000  20000
3  2       1000  21000
4  2     -11000  10000

My desired result is following.

I would like to get running sum only in status = 2 after another status code.

no status amount running_sum
1  2      10000  10000
2  3      10000  20000
3  2       1000  11000
4  2     -11000  0

How to add conditioning in my formula ?

Are there any good way to achieve this?

Thanks

答案1

得分: 1

你可以使用以下代码来完成:

使用 `sum(amount) over(partition by status order by no)` 这段代码

    选择
        主要.no
        , 主要.状态
        , 主要.金额
        , SUM(主要.金额) OVER ( 
            按状态分组
            按主要.no排序 行在无限制之前和当前行之间
        ) as 累计总额
    来自
        mytable AS 主要
    no排序

[在此处查看演示][1]
英文:

You can do it using sum(amount) over(partition by status order by no)

SELECT
    main.no
    , main.status
    , main.amount
    , SUM(main.amount) OVER ( 
        partition by status
        ORDER BY
            main.no ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) as running_sum
from
    mytable AS main
order by no

Demo here

答案2

得分: 1

根据您的预期输出,在 status = 2 时,您只需从您的累加总和中减去 status <> 2 的情况下的金额的累加总和,尝试以下操作:

select main.no,
       main.status,
       main.amount,
       sum(main.amount) over (order by main.no) - 
       case when main.status = 2 
         then sum(case when main.status <> 2 then main.amount else 0 end) over (order by main.no) 
         else 0 
       end as running_sum
from table_name as main

演示

英文:

According to your expected output, when status = 2, you just need to subtract the running sum of the amount where status <> 2 from your running sum, try the following:

select main.no,
       main.status,
       main.amount,
       sum(main.amount) over (order by main.no) - 
       case when main.status = 2 
         then sum(case when main.status <> 2 then main.amount else 0 end) over (order by main.no) 
         else 0 
       end as running_sum
from table_name as main

Demo

huangapple
  • 本文由 发表于 2023年3月7日 17:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定