如何在ORACLE中累积特定列并在特定条件下释放值

时间:2018-09-14 23:29:04

标签: sql oracle

我的下表如下:

ID        ACTION_DATE              AMOUNT   ACTION_TYPE
--------------------------------------------------------
1   01-JAN-18 12.37.44.232000000 AM 24  Dr
1   01-JAN-18 12.38.16.928000000 AM 30  Dr
1   01-JAN-18 01.20.18.435000000 AM 200 Cr
1   01-JAN-18 01.35.43.462000000 AM 15  Dr
1   01-JAN-18 01.43.42.925000000 AM 30  Dr
1   01-JAN-18 01.49.23.746000000 AM 23  Dr
1   01-JAN-18 01.49.52.190000000 AM 26  Dr
2   01-JAN-18 12.37.44.232000000 AM 40  Dr
2   01-JAN-18 12.38.16.928000000 AM 50  Dr
2   01-JAN-18 01.20.18.435000000 AM 100 CR
2   01-JAN-18 01.35.43.462000000 AM 15  Dr
2   01-JAN-18 01.43.42.925000000 AM 20  Dr
2   01-JAN-18 01.49.23.746000000 AM 25  Dr
2   01-JAN-18 01.49.52.190000000 AM 30  Dr

,目标表将为

ID  ACTION_DATE                 AMOUNT  ACTION_TYPE Acc_amount
---------------------------------------------------------------
1   01-JAN-18 12.37.44.232000000 AM 24  Dr  24
1   01-JAN-18 12.38.16.928000000 AM 30  Dr  54
1   01-JAN-18 01.20.18.435000000 AM 200 Cr  0
1   01-JAN-18 01.35.43.462000000 AM 15  Dr  15
1   01-JAN-18 01.43.42.925000000 AM 30  Dr  45
1   01-JAN-18 01.49.23.746000000 AM 23  Dr  68
1   01-JAN-18 01.49.52.190000000 AM 26  Dr  84
2   01-JAN-18 12.37.44.232000000 AM 40  Dr  40
2   01-JAN-18 12.38.16.928000000 AM 50  Dr  90
2   01-JAN-18 01.20.18.435000000 AM 100 CR  0
2   01-JAN-18 01.35.43.462000000 AM 15  Dr  15
2   01-JAN-18 01.43.42.925000000 AM 20  Dr  35
2   01-JAN-18 01.49.23.746000000 AM 25  Dr  60
2   01-JAN-18 01.49.52.190000000 AM 30  Dr  90

我尝试使用overpartition by作为

select id, action_date,action_type, amount, 
sum(amount) over (partition by id order by action_date) as acc_amount
from cashanalysis  order by id,action_date;

一旦action_type ='Cr',如何将acc_amount释放为零

感谢您的帮助

0 个答案:

没有答案