我想将所有借记和借方金额中的贷方相加。 比如从前两行汇总借记金额并从第3,4行减去信用额度并显示在ttl blnc列中,但是下面的结果不正确它只是从借方扣除贷方
我需要这个结果
debit Credit Running Total
144 0 144
264 0 408
0 264 144
0 441 -297
2464 0 2167
144 0 2311
0 27306 -24995
264 0 -24731
这是我的代码
result number:=0;
begin
result:= result + debit - credit;
return result;
答案 0 :(得分:1)
使用此formulla
RETURN(NVL(:CF_ttl_blnc,0)+NVL(:debit,0)-NVL(:credit,0));
答案 1 :(得分:0)
假设您希望逐行查看运行总计,我会使用SUM()分析功能。
SELECT
xti.dr_amount
,xti.cr_amount
,SUM(dr_amount-cr_amount) OVER (ORDER BY xti.invoice_num) running_total
FROM
xxcjp_test_invoices xti
ORDER BY
xti.invoice_num
;
使用以下命令创建测试数据
CREATE TABLE XXCJP_TEST_INVOICES
(invoice_num VARCHAR2(10)
,dr_amount NUMBER
,cr_amount NUMBER
)
;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A001',144,0) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A002',264,0) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A003',0,264) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A004',0,441) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A005',2464,0) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A006',144,0) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A007',0,27306) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A008',264,0) ;
INSERT INTO XXCJP_TEST_INVOICES VALUES ('A009',55500,0) ;