0
with account as
(
select 'client1' as client, to_date('09.2009' ,'MM.YYYY') as months, '09_1' as bill_num, 100 as BF_sum, 400 as Payed_SUM from dual
union
select 'client1' as client, to_date('09.2009' ,'MM.YYYY') as months, '09_2' as bill_num, 150 as BF_sum, 50 as Payed_SUM from dual
union
select 'client1' as client, to_date('10.2009' ,'MM.YYYY') as months, '10_1' as bill_num, 150 as BF_sum, 50 as Payed_SUM from dual
union
select 'client1' as client, to_date('11.2009' ,'MM.YYYY') as months, '11_1' as bill_num, 150 as BF_sum, 0 as Payed_SUM from dual
union
select 'client1' as client, to_date('12.2009' ,'MM.YYYY') as months, '12_1' as bill_num, 150 as BF_sum, 100 as Payed_SUM from dual
)
select client, months, BF_sum, Payed_SUM, KREDIT, KREDIT+lag(KREDIT,1, 0) over (partition by client order by months) as lead_val
from (
select client, months, sum(BF_sum) as BF_sum, sum(Payed_SUM) as Payed_SUM, (sum(Payed_SUM) - sum(BF_sum)) as KREDIT
from account
group by client, months
)
order by client, months
正如你看到的,我得到了以下如果使用LAG FUNC(?):進算了一筆賬(報告)需要諮詢 - LAG FUNC
CLIENT MONTHS BF_SUM PAYED_SUM KREDIT LEAD_VAL
client1 01.09.2009 0:00:00 250 450 200 200
client1 01.10.2009 0:00:00 150 50 -100 100
client1 01.11.2009 0:00:00 150 0 -150 -250
client1 01.12.2009 0:00:00 150 100 -50 -200
,我需要用SUM從09.2009(Kredit = 200)支付10.2009票據和其他賬單如果錢仍然離開。 所以我想結果:
client months BF_SUM PAYED_SUM KREDIT NEW_KREDIT_MONTHS
client1 '09.2009' 250 450 200 0
client1 '10.2009' 150 50 -100 0
client1 '11.2009' 150 0 -150 -50
client1 '12.2009' 150 100 -50 -50
也許我需要通過所有不納了款項連接到「週期」,直到Kredit結束???
任何想法,傢伙?
太棒了!非常感謝! 沒有考慮使用「客戶端總分區」來運行總數! – zmische 2009-12-17 07:13:14