简体   繁体   中英

Need to calculate running balance of debit

I am working for 1 accounts project and i have stuck in 1 place.

Table is debit

ID      Debit       Credit      A       B       C   
1       1000.00     900.00      0       0       1000.00     
2       450.00      425.00      0       450.00  0   
3       500.00      490.00      500.00  0       0   
4       600.00      599.00      600.00  0       0   
5       748.00      700.00      0       748.00  0   


Now if we sum the credit it will be = 3114,  
What I have to do here is whatever total credit I have it has to start from top (A+B+C) - 3114  
So It will make C = 0 and my new credit will be 3114-1000=2114,   
Then in my id=2 it will do the same thing (A+B+C) - 2114  
so now B will be 0 and my new credit will be 2114-450=1664

My end output after all the calculation it should be

ID      Debit       Credit      A       B       C   
1       1000.00     900.00      0       0       0.00        
2       450.00      425.00      0       0.00    0   
3       500.00      490.00      0.00    0       0   
4       600.00      599.00      0.00    0       0   
5       748.00      700.00      0       184.00  0   

If we try to (sum A + Sum B + Sum C) - credit, figure will be the same but it will come in column A, So after all the brainstorming I think above calculation has to be applied.

Do anyone has any idea how to achieve this.

Any help would be appreciated.

SQL Server only support analytics function SUM() OVER from version MSSQL 2012, so for 2008 this may be one way to query your result:

WITH table_name AS
( 
    SELECT 1 ID, 000.00 Debit, 900.00 Credit, 0       a, 0       b, 1000.00     c UNION ALL
    SELECT 2 ID, 450.00 Debit, 425.00 Credit, 0       a, 450.00  b, 0   c UNION ALL
    SELECT 3 ID, 500.00 Debit, 490.00 Credit, 500.00  a, 0       b, 0   c UNION ALL
    SELECT 4 ID, 600.00 Debit, 599.00 Credit, 600.00  a, 0       b, 0   c UNION ALL
    SELECT 5 ID, 748.00 Debit, 700.00 Credit, 0       a, 748.00  b, 0   c 
)
, 
sum_credit AS
(
    SELECT SUM(credit) sumcredit 
    FROM table_name
)
SELECT t.id, t.debit, t.credit, 
    CASE WHEN a = 0
            OR ( (SELECT COALESCE(SUM(a + b + c), 0) FROM table_name WHERE id < t.id)
                + a - sc.sumcredit < 0
            )
        THEN 0 
        ELSE (SELECT COALESCE(SUM(a + b + c), 0) FROM table_name WHERE id < t.id)
            + a - sc.sumcredit 
        END a,
    CASE WHEN b = 0
            OR ( (SELECT COALESCE(SUM(a + b + c), 0) FROM table_name WHERE id < t.id)
                + a + b - sc.sumcredit < 0
            )
        THEN 0 
        ELSE (SELECT COALESCE(SUM(a + b + c), 0) FROM table_name WHERE id < t.id)
            + a + b - sc.sumcredit 
        END b,
    CASE WHEN c = 0 
            OR ( (SELECT COALESCE(SUM(a + b + c), 0) FROM table_name WHERE id < t.id)
                + a + b + c - sc.sumcredit < 0
            )
        THEN 0 
        ELSE (SELECT COALESCE(SUM(a + b + c), 0) FROM table_name WHERE id < t.id)
            + a + b + c - sc.sumcredit 
        END c
FROM 
    table_name t
CROSS JOIN 
    sum_credit sc;

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