calculate running balance in oracle query

时光怂恿深爱的人放手 提交于 2019-11-29 06:57:51
Boneist

I think you're after:

SELECT   B.NO_KLAIM AS id,
         a.amount AS amount_a,
         B.AMOUNT AS amount_b,
         a.amount - SUM (B.AMOUNT) 
                   OVER (partition by b.no_klaim ORDER BY B.cp_id ROWS UNBOUNDED PRECEDING)
              AS running_balance
FROM      TRX_TITIPAN A
           JOIN
              TRX_KLAIM_TITIPAN B
           ON A.NO_RESI_TITIPAN = B.NO_RESI_TITIPAN

It would be much easier if you attach description of your tables.

From sample data you provided you need:

select id, amout_a, amount_b, 
       amount_a - sum(amount_b) over (partition by id order by id, cp_id) as running_balance 
  from table;

Trying to translate it into your tables it gives:

SELECT B.NO_KLAIM AS id,
       a.amount AS amount_a,
       B.AMOUNT AS amount_b,
       a.amount - SUM(B.AMOUNT) OVER (PARTITION BY B.NO_KLAIM ORDER BY B.NO_KLAIM/*, HERE PUT WHAT IS cp_id*/) AS running_balance
FROM      TRX_TITIPAN A
       JOIN
          TRX_KLAIM_TITIPAN B
       ON A.NO_RESI_TITIPAN = B.NO_RESI_TITIPAN

You just need to fill one column in order by which stands for cp_id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!