2016-01-05 111 views
0

我在我的數據庫表中的以下記錄:計算平衡

Date   Credit Debit  Description 
--------------- ------- ------- --------------- 
12-24-2015  5     Purchased credit 
12-20-2015    1   Consumed credit 
12-15-2015  3     Purchased credit 
12-08-2015    1   Consumed credit 
12-08-2015    1   Consumed credit 
12-07-2015    1   Consumed credit 
12-04-2015    1   Consumed credit 
12-03-2015    1   Consumed credit 
12-01-2015  5     Purchased credit 

我想要計算並顯示每個記錄的餘額如下圖所示:

Date   Credit Debit Balance Description 
------------ ------- ------- ------- --------------- 
12-24-2015 5  0  7  Purchased credit 
12-20-2015   1  2  Consumed credit 
12-15-2015 3  0  3  Purchased credit 
12-08-2015   1  0  Consumed credit 
12-08-2015   1  1  Consumed credit 
12-07-2015   1  2  Consumed credit 
12-04-2015   1  3  Consumed credit 
12-03-2015   1  4  Consumed credit 
12-01-2015 5  0  5  Purchased credit 

能任何人都可以幫助我實現上述結果?

+0

它已經張貼在的問題。第一部分顯示可用數據,第二部分顯示預期結果 –

回答

3

爲了產生分析天平版使用sum()

select tdate, credit, debit, 
     sum(nvl(credit, 0)-nvl(debit, 0)) over (order by rn) balance, description 
    from (
    select tdate, credit, debit, row_number() over (order by tdate) rn, description 
     from test) 
    order by rn desc 

如果您的表格包含增加的主鍵,則可以使用此代替生成的行號。

測試數據和輸出:

create table test (tdate date, credit number(6), debit number(6), description varchar2(20)); 
insert into test values (date '2015-12-24', 5, null, 'Purchased credit'); 
insert into test values (date '2015-12-20', null, 1, 'Consumed credit'); 
insert into test values (date '2015-12-15', 3, null, 'Purchased credit'); 
insert into test values (date '2015-12-08', null, 1, 'Consumed credit'); 
insert into test values (date '2015-12-08', null, 1, 'Consumed credit'); 
insert into test values (date '2015-12-07', null, 1, 'Consumed credit'); 
insert into test values (date '2015-12-04', null, 1, 'Consumed credit'); 
insert into test values (date '2015-12-03', null, 1, 'Consumed credit'); 
insert into test values (date '2015-12-01', 5, null, 'Purchased credit'); 

TDATE  CREDIT DEBIT BALANCE DESCRIPTION 
----------- ------- ------- ---------- -------------------- 
2015-12-24  5     7 Purchased credit 
2015-12-20    1   2 Consumed credit 
2015-12-15  3     3 Purchased credit 
2015-12-08    1   0 Consumed credit 
2015-12-08    1   1 Consumed credit 
2015-12-07    1   2 Consumed credit 
2015-12-04    1   3 Consumed credit 
2015-12-03    1   4 Consumed credit 
2015-12-01  5     5 Purchased credit 
3

您應該可以使用分析函數LAG來查看上一行的數據。

SELECT Date, 
     Credit, 
     Debit, 
     LAG(Balance, 1, 0) OVER(ORDER BY Date) - Debit + Credit AS Balance, 
     Description 
    FROM sometable 

1參數意味着它看起來1行前,所述0參數意味着如果該行的給定偏移量不存在,則返回0,而不是(即,用於第一行)。

來源:https://oracle-base.com/articles/misc/lag-lead-analytic-functions

+0

我不相信這是一個問題,因爲信用額正被添加到0. –

0

爲了試試這個,首先做一個自我加入,然後列出了結果:

SELECT a1.Date, a1.Debit, a1.Credit, SUM(a2.Debit-a2.Credit) Balance, Description 
FROM table a1, table a2 
WHERE a1.debit<=a2.debit and a1.credit<=a2.credit OR (a1.debit=a2.debit and a1.credit=a2.credit and a1.date = a2.date) 
GROUP BY a1.Date, a1.Debit, a1.Credit 
ORDER BY a1.Date DESC; 
+0

自加入不起作用 –

+0

輸出是什麼? –

+0

沒有輸出顯示 –