迭代回来以调整for循环中的计算值 [英] iterate back to adjust the calculated values in for loop

查看:183
本文介绍了迭代回来以调整for循环中的计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PLSQL程序来计算分期付款系统的运行余额.当余额不等于零时,我面临剩余余额的问题,我想将其调整为以前的分期付款金额

I have writing a PLSQL Program to Calculate the Running Balance for an Installment System.I am facing a issue on the Remaining balance when it is not equal to zero i want to adjust it to previous Installment amounts

请参见以下代码,因为您可以在任何Plsql编辑器中对其进行测试

Please see the below code as you can test it any Plsql editor

DECLARE
   l_bal_flg              VARCHAR2 (10) := 'Y';
   l_install_amt          NUMBER := 1362;
   l_calcaulatedbalance   NUMBER := 59024;
   l_curr_bal             NUMBER := 0;
   l_residual_amt         NUMBER := 14125;
   l_installment_cnt      NUMBER := 34;
   l_install_seq          NUMBER := 1;
BEGIN
   FOR j IN 1 .. l_installment_cnt LOOP
      IF l_residual_amt <> 0 THEN
         IF l_installment_cnt = l_install_seq THEN
            l_install_amt := l_residual_amt;
         END IF;
      END IF;


      IF l_bal_flg = 'Y' THEN
         l_curr_bal := l_calcaulatedbalance - l_install_amt;
         l_bal_flg := 'N';
      ELSE
         l_curr_bal := l_curr_bal - l_install_amt;


      END IF;

      l_install_seq := l_install_seq + 1;

      DBMS_OUTPUT.PUT_LINE (l_install_seq||' '||l_install_amt || '  ' || l_curr_bal);


   END LOOP;
END;

输出

1 1362  57662
2 1362  56300
3 1362  54938
4 1362  53576
5 1362  52214
6 1362  50852
7 1362  49490
8 1362  48128
9 1362  46766
10 1362  45404
11 1362  44042
12 1362  42680
13 1362  41318
14 1362  39956
15 1362  38594
16 1362  37232
17 1362  35870
18 1362  34508
19 1362  33146
20 1362  31784
21 1362  30422
22 1362  29060
23 1362  27698
24 1362  26336
25 1362  24974
26 1362  23612
27 1362  22250
28 1362  20888
29 1362  19526
30 1362  18164
31 1362  16802
32 1362  15440
33 1362  14078
34 14125  -47

我想如下将-47调整回第33行

I want to Adjust back -47 to Row 33 as below

33 1362+(-47)  14078
34 14125       0

如果-47为-47或其他一些大于1362的值(例如,示例剩余值为1500,那么将会发生

if -47 is or some other value greater than 1362 for Example remaining value is 1500 then what will happen

32 1362+(-138)   15440
33 1362+(-1362)  14078
34 14125         0

推荐答案

以下代码段应按照您要求的逻辑工作..

following code snippet should work as per the logic required by you..

DECLARE
   l_bal_flg              VARCHAR2 (10) := 'Y';
   l_install_amt          NUMBER := 1362;
   l_calcaulatedbalance   NUMBER := 59024;
   l_curr_bal             NUMBER := 0;
   l_tot_inst_amount             NUMBER := 0;
   l_last_bal_amount             NUMBER := 0;
   l_install_amount2             NUMBER := 0;

   l_last_bal_amount2             NUMBER := 0;
   l_residual_amt         NUMBER := 14125;
   l_installment_cnt      NUMBER := 34;
   l_install_seq          NUMBER := 1;
   l_bal_trans            NUMBER :=0;
   l_bal_trans_cnt            NUMBER :=0;
BEGIN
-- amount to be balanced at last before transaction
l_tot_inst_amount := l_calcaulatedbalance-l_residual_amt;
l_last_bal_amount := round(((l_tot_inst_amount/(l_installment_cnt-1)) -  l_install_amt) * (l_installment_cnt-1));

-- to get the transaction from which it has to be balanced
IF l_last_bal_amount <> 0
THEN
   l_last_bal_amount2 :=   ABS(l_last_bal_amount);
   l_install_amount2  :=   l_install_amt;
   l_BAL_TRANS := ceil(l_last_bal_amount2/l_install_amt);
   -- to get the amount to be added on first transaction to be balanced
loop
exit when l_last_bal_amount2 < l_install_amt;
l_last_bal_amount2 := l_last_bal_amount2 - l_install_amt;
end loop;

-- to get the exact transaction count on which first balance to be made
l_bal_trans_cnt :=l_installment_cnt-l_bal_trans;
END IF;

   FOR j IN 1 .. l_installment_cnt LOOP

-- adjustment at the transactions to be balanced
         IF l_bal_trans_cnt = l_install_seq THEN
            l_install_amt := l_install_amount2+(l_last_bal_amount2*(l_last_bal_amount/ABS(l_last_bal_amount)));

        elsif l_install_seq > l_bal_trans_cnt  and l_last_bal_amount >0  then
        l_install_amt := l_install_amount2+l_install_amount2;
         END IF;  



      IF l_residual_amt <> 0 THEN
         IF l_installment_cnt = l_install_seq THEN
            l_install_amt := l_residual_amt;
         END IF;
         -- if balance to be paid is the residual amount then no installment to be paid for the current month 
         IF l_curr_bal = l_residual_amt and l_installment_cnt <> l_install_seq THEN
            l_install_amt := 0;
         END IF;         
      END IF;


      IF l_bal_flg = 'Y' THEN
         l_curr_bal := l_calcaulatedbalance - l_install_amt;
         l_bal_flg := 'N';
      ELSE
         l_curr_bal := l_curr_bal - l_install_amt;
      END IF;

      l_install_seq := l_install_seq + 1;

      DBMS_OUTPUT.PUT_LINE (l_install_seq||' '||l_install_amt || '  ' || l_curr_bal);


   END LOOP;
END;


-- output:
for calculated balance 59024
34 1315  14125
35 14125  0

for calculated balance 63024
31 1362  22164
32 2591  19573
33 2724  16849
34 2724  14125
35 14125  0

for calculated balance 55024
31 1362  14164
32 39  14125
33 0  14125
34 0  14125
35 14125  0

这篇关于迭代回来以调整for循环中的计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆