Make cumulative sum in view oracle

后端 未结 2 790
难免孤独
难免孤独 2021-01-23 13:44

I want to make a view in oracle, how can I get value of \'saldo\' that I supposed like in this screenshoot with oracle? can someone give me the script?

The result that

2条回答
  •  时光说笑
    2021-01-23 14:08

    There's a bunch of things you can do with Oracle Analytics functions, especially made for that purpose.

    Oracle Analytic functions

    You can also use the PARTITION BY to have cumul for the day or the period:

    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 
    

提交回复
热议问题