我想計算整個財政年度的財務餘額利息。在分類賬表中有條目時,我可以在特定的日期獲得運行餘額,但是當沒有條目時,我的利息計算就會失靈。下面是分類帳表:計算利息的計算總額
OOWNCODE DTOFTRAN VOUCHER DRCR DAMOUNT CAMOUNT
00100VS055 04/01/2013 OPNG001NSE0 Debit 1303.87 0.00
00100VS055 05/15/2013 JVOFSET0000025 Debit 337.08 0.00
下面是正在運行的分類帳餘額查詢:
Select ledger.oowncode as "Clientcode",
to_char(ledger.dtoftran, 'DD/MM/YYYY') as "Date",
sum(ledger.damount - ledger.camount) as "Ledger",
Sum(Sum(Ledger.Damount - Ledger.Camount)) over(Partition by Ledger.Oowncode Order by Ledger.Oowncode,ledger.dtoftran rows between unbounded preceding and current row) as "Running Balance",
round((Sum(Sum(Ledger.Damount - Ledger.Camount)) over(Partition by Ledger.Oowncode Order by Ledger.Oowncode,ledger.dtoftran rows between unbounded preceding and current row)*18/365),2) as "Interest"
from ledger ledger
where ledger.firmnumber = 'SUG-000001'
and ledger.oowncode = '00100VS055'
and ledger.dtoftran >='01-APR-2013'
and ledger.dtoftran <='31-MAY-2013'
group by ledger.oowncode, ledger.dtoftran
並且在下面的結果:
Clientcode Date Ledger Running BalanceInterest
00100VS055 01/04/2013 1303.87 1303.87 64.3
00100VS055 15/05/2013 337.08 1640.95 80.92
現在邏輯,運行的1303.87的平衡每天運行餘額直到15/05/2013,然後1640.95是未來幾天的運行餘額。我想要的結果是這樣的:
Clientcode Date Ledger Running Balance Interest
00100VS055 01/04/2013 1303.87 1303.87 64.3
00100VS055 02/04/2013 1303.87 1303.87 64.3
00100VS055 03/04/2013 1303.87 1303.87 64.3
00100VS055 04/04/2013 1303.87 1303.87 64.3
等多達15直到2013年5月
.
00100VS055 15/05/2013 337.08 1640.95 80.92
能否請你幫我在這地上?
嗨大衛,
按照您的幫助,我已經做了必要的編碼:
WITH l AS
(SELECT oowncode,dtoftran,damount,camount from ledger
WHERE firmnumber='DDA-000001' and oowncode='ACCOPEN'
GROUP BY oowncode,dtoftran,damount,camount)
Select l.oowncode,cte_dates.dtoftran,sum(l.damount-l.camount) running
From (SELECT dtoftran
FROM ledger
WHERE dtoftran BETWEEN
TO_DATE('01/04/2011', 'DD/MM/YY')
AND TO_DATE('31/03/2012', 'DD/MM/YY') group by dtoftran) cte_dates
left outer join l
partition by (oowncode) on (cte_dates.dtoftran = l.dtoftran)
group by l.oowncode,cte_dates.dtoftran
及以下的輸出:
1 ACCOPEN 01/04/2011 12350
2 ACCOPEN 02/04/2011
3 ACCOPEN 04/04/2011
4 ACCOPEN 05/04/2011
5 ACCOPEN 06/04/2011
6 ACCOPEN 07/04/2011 2600
7 ACCOPEN 08/04/2011 -650
8 ACCOPEN 09/04/2011
9 ACCOPEN 11/04/2011
10 ACCOPEN 12/04/2011
11 ACCOPEN 13/04/2011 650
12 ACCOPEN 14/04/2011
13 ACCOPEN 15/04/2011 9100
14 ACCOPEN 16/04/2011
我甚至想對02/04/2011應該引入開窗條款的運行總數。你能指導我,我錯在哪裏?
嗨大衛感謝您的指導,但不知何故,即時通訊不能做同樣的你能提供給我查詢,在此先感謝 – user3064554
你能幫忙我關於錯過日期和運行總數 – user3064554