简体   繁体   中英

How to calculate debit and credit running balance with lag function

i have some data in my table accounting .

     ID EDATE     DISCRIPTION      DR         CR     
    --- --------- ------------- ---------- ------  
     1 19-JAN-19 cash in           1000       0       
     2 19-JAN-19 cash out             0     200 
     3 19-JAN-19 cash in            500       0       
     4 19-JAN-19 cash out             0     200        
     5 19-JAN-19 cash out             0     200        
     6 19-JAN-19 cash out             0    1800

I want to get running balance of debit and credit balance like as below

     ID      EDATE    DISCRIPTION      DR  CR      BALANCE
    --- --------- -------------     ------ ------ ----------
     1 19-JAN-19 cash in           1000       0       1000dr
     2 19-JAN-19 cash out             0     200        800dr
     3 19-JAN-19 cash in            500       0       1300dr
     4 19-JAN-19 cash out             0     200       1100dr 
     5 19-JAN-19 cash out             0     200        900dr
     6 19-JAN-19 cash out             0    1800      (900)cr

i have tried to do this with LAG FUNCITON but failed my code is below

select id,edate,discription,dr,cr,
dr-lag(dr,1,0)
over(order by id) as balance 
from accounting;

And my output is

ID   EDATE     DISCRIPTION             DR     CR    BALANCE
--- --------- -------------           ------- ----  -------
     1 19-JAN-19 cash in                    1000    0    1000
     2 19-JAN-19 cash out                      0  200   -1000
     3 19-JAN-19 cash in                     500    0     500
     4 19-JAN-19 cash out                      0  200    -500
     5 19-JAN-19 cash out                      0  200       0

You can try to use sum window function.

CREATE TABLE accounting(
   ID int,
   EDATE varchar(50),
   DISCRIPTION varchar(50),
   DR int,
   CR int
);


INSERT INTO accounting VALUES (1,'19-JAN-19','cash in',1000,0);       
INSERT INTO accounting VALUES (2,'19-JAN-19','cash out',0,200); 
INSERT INTO accounting VALUES (3,'19-JAN-19','cash in',500,0);       
INSERT INTO accounting VALUES (4,'19-JAN-19','cash out',0,200);        
INSERT INTO accounting VALUES (5,'19-JAN-19','cash out',0,200);        
INSERT INTO accounting VALUES (6,'19-JAN-19','cash out',0,1800);

Query 1 :

select 
  id,
  edate,
  discription,
  dr,
  cr,
  sum(DR) over(order by id) - sum(CR) over(order by id)  as balance 
from accounting

Results :

| ID |     EDATE | DISCRIPTION |   DR |   CR | BALANCE |
|----|-----------|-------------|------|------|---------|
|  1 | 19-JAN-19 |     cash in | 1000 |    0 |    1000 |
|  2 | 19-JAN-19 |    cash out |    0 |  200 |     800 |
|  3 | 19-JAN-19 |     cash in |  500 |    0 |    1300 |
|  4 | 19-JAN-19 |    cash out |    0 |  200 |    1100 |
|  5 | 19-JAN-19 |    cash out |    0 |  200 |     900 |
|  6 | 19-JAN-19 |    cash out |    0 | 1800 |    -900 |

Use format S and replace the standard +/- signs with your required codes

You may ommit the NVL if your number columns are not nullable.

The aggregated SUM function is often used with PARTITION BY to distinct the accounts - see the comment in the query.

with bal as (
select 
  id, cr, dr,
  sum(nvl(cr,0) - nvl(dr,0)) over (/* PARTITION BY account key */ ORDER BY id) as balance
from accounting)
select 
 id, dr db,cr,
 replace(replace(to_char(balance,'999,999.99S'),'+','CR'),'-','DB') balance
from bal 
order by id;

        ID         DB         CR BALANCE                                    
---------- ---------- ---------- ----------------
         1       1000          0   1,000.00DB                                 
         2          0        200     800.00DB                                 
         3        500          0   1,300.00DB                                 
         4          0        200   1,100.00DB                                 
         5          0        200     900.00DB                                 
         6          0       1800     900.00CR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM