我想计算整个财政年度的财务余额利息。当分类帐表中有条目时,我能够获得特定日期的运行余额,但是当没有条目时,我的利息计算会变得混乱。下面是分类帐表:
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正在每天运行余额,直到2013年5月15日,然后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
依此类推,直到2013年5月15日。
.
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它应该引入窗口条款的运行总计。你能指导我哪里错了吗?
答案 0 :(得分:2)
这是一个数据密集化问题,您需要确保每个可能值都有一个记录 - 在您的情况下,Ledger.Oowncode和ledger.dtoftran的每个值。
Oracle 10g具有用于此目的的分区连接语法:http://docs.oracle.com/cd/B19306_01/server.102/b14223/analysis.htm#i1014934
在您的情况下,您使用查询生成日期列表,您将外部联接分区到分类帐表。通过oowncode进行分区并加入日期。这将使数据密集化,并且您应用窗口函数来执行计算。