Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 累进账户计算(报告)需要建议-滞后功能(?)_Sql_Oracle - Fatal编程技术网

Sql 累进账户计算(报告)需要建议-滞后功能(?)

Sql 累进账户计算(报告)需要建议-滞后功能(?),sql,oracle,Sql,Oracle,如您所见,如果使用LAG func,我会得到以下结果: 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' ,'M

如您所见,如果使用LAG func,我会得到以下结果:

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
我需要用2009年9月的金额(Kredit=200)支付10.2009年的账单和其他账单,如果还有剩余的钱。 所以我想知道结果:

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
也许我需要通过连接“循环”所有未支付的金额,直到Kredit结束


有什么想法吗,伙计们?

我不能完全确定我是否理解你们的业务逻辑。这在您的示例中给出了结果

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总数。然后将正值替换为0,因为我认为您只对负值感兴趣。然后计算一个月到下一个月总数的变化量,这是我对你要找的东西的最佳猜测

太棒了!谢谢!我没想过用“客户机和分区”来运行total!
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