在oracle中查看累积总和

时间:2015-09-18 07:42:45

标签: oracle view

我想在oracle中查看一个视图,我怎样才能在这个使用oracle的屏幕截图中得到'saldo'的值?有人可以给我剧本吗?

我想要的结果:

dateacct   period_id   startdate   enddate     debit   credit   saldo
7/10/2015     1        1/10/2015   30/10/2015   25       0        25
8/10/2015     1        1/10/2015   30/10/2015   0        5        20
9/10/2015     1        1/10/2015   30/10/2015   0        3        17
10/10/2015    1        1/10/2015   30/10/2015   4        0        21

注意:dateacct,period_id,startdate,enddate,credit,debit是我从'akun'表中获取的列。

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT DATEACCT, PERIOD_ID, STARTDATE, ENDDATE, CREDIT, DEBIT,
    SUM (DEBIT-CREDIT) OVER (ORDER BY DATEACCT,PERIOD_ID,STARTDATE,ENDDATE) saldo
from AKUN
ORDER BY DATEACCT, PERIOD_ID, STARTDATE, ENDDATE;

答案 1 :(得分:1)

您可以使用Oracle Analytics功能执行许多操作,尤其是为此目的而制作的。

Oracle Analytic functions

您还可以使用PARTITION BY获得当天或期间的累积奖励:

SELECT DATEACCT, PERIOD_ID, STARTDATE, ENDDATE, CREDIT, DEBIT,  
    SUM (DEBIT-CREDIT) OVER (PARTITION BY DATEACCT ORDER BY DATEACCT,PERIOD_ID,STARTDATE,ENDDATE) CUMULDAY,
    SUM (DEBIT-CREDIT) OVER (PARTITION BY PERIOD_ID ORDER BY DATEACCT,PERIOD_ID,STARTDATE,ENDDATE) CUMULPERIOD
from AKUN
ORDER BY DATEACCT, PERIOD_ID, STARTDATE, ENDDATE;



DATEACCT  PERIOD_ID STARTDATE ENDDATE      CREDIT      DEBIT   CUMULDAY    CUMULPERIOD
-------- ---------- --------- -------- ---------- ---------- ---------- -----------
07/10/15          1 01/10/15  30/10/15          0         25         25          25 
08/10/15          1 01/10/15  30/10/15          5          0         -5          20 
09/10/15          1 01/10/15  30/10/15          3          0         -3          17 
10/10/15          1 01/10/15  30/10/15          0          4          4          21