假設您想要逐行查看正在運行的總數,我會使用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) ;