2016-04-06 59 views
1

我想對所有借方進行彙總,並從借方金額中逐行減去貸方。從上面兩行 喜歡和借方發生額,並從3,4行顯示減去信貸TTL blnc列,但下面的結果不正確,它只是從借記卡中減去信用如何製作借方和貸方公式

enter image description here

我需要這個結果

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; 

回答

1

使用本formulla

RETURN(NVL(:CF_ttl_blnc,0)+NVL(:debit,0)-NVL(:credit,0)); 
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) ;