简体   繁体   English

需要计算借方的余额

[英]Need to calculate running balance of debit

I am working for 1 accounts project and i have stuck in 1 place. 我正在为1个帐户项目工作,但我停留在1个地方。

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. 如果我们尝试(总和A +总和B +总和C)-贷方,数字将相同,但将出现在A列中。因此,在进行所有头脑风暴之后,我认为必须应用上述计算。

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: SQL Server仅支持MSSQL 2012版本中的分析函数SUM() OVER ,因此对于2008年,这可能是查询结果的一种方法:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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