累进帐户计算(报告)需要建议 - LAG功能(?)

时间:2009-12-16 17:46:06

标签: sql oracle

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功能,我会得到以下信息:

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

如果Money仍然存在,我需要使用2009年9月9日的赔率(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

也许我需要Connect by来“循环”所有未支付的金额,直到Kredit结束?

任何想法,伙计们?

1 个答案:

答案 0 :(得分: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替换正值,因为我认为你只对看到负数感兴趣。然后计算总数已经改变一个月到下一个月的数量,这是我对你要找的东西的最佳猜测