2009-12-16 38 views
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結束???

任何想法,傢伙?

回答

1

我不完全確定我瞭解您的業務邏輯。這雖然給你的例子的結果。

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, kredit, amount_short - lag(amount_short,1,0) over (partition by client order by months) new_kredit_months 
from (
select client, months, kredit, least(cumulative_kredit,0) amount_short 
from (
select client, months,kredit,sum(kredit) over (partition by client order by months) cumulative_kredit 
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 

首先我得到kredit的累計運行總數。然後用0替換正值,因爲我認爲你只對看到負數感興趣。然後計算總額已經改變了一個月的數額,這是我最好的猜測,你正在尋找什麼

+0

太棒了!非常感謝! 沒有考慮使用「客戶端總分區」來運行總數! – zmische 2009-12-17 07:13:14