暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 使用SQL计算复利

askTom 2016-09-12
810

问题描述

嗨,

我在尝试创造一个逻辑。请帮帮我。

假设我在2015年1月带了一只基金,它将在2018年到期,我每半年收到利息。
我想核对一下从10月16日至9月17日收到的利息。
基本上我应该在12月16日和6月17日收到利息。
你能帮我发展一下这个逻辑吗。

谢谢你。

专家解答

所以你想把利息复利计算成两个时期的还款额?

如果是这样,您需要使用复利公式。这就是:

A = P (1 + r/n) ^ nt


Where:

A = the future value of the investment/loan, including interest
P = the principal investment amount (the initial deposit or loan amount)
r = the annual interest rate (decimal)
n = the number of times that interest is compounded per year
t = the number of years the money is invested or borrowed for


http://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php

要计算出基金在某一给定时刻的价值,你可以把这些数字插入公式中。要得到付款,减去前一次付款的价值。

例如,如果基金的起始值为100 ,回报为5% ,则以下公式将计算第四次付款的值(在12月16日) :

begin
  dbms_output.put_line(
    round(( 100 * power ( ( 1 + (0.05/2) ), 4) ), 2) -
    round(( 100 * power ( ( 1 + (0.05/2) ), 3) ), 2)
  );
end;
/

2.69


或者,您可以编写一些SQL来计算所有期间的值。这需要一个运行的产品。没有内建的功能来完成此操作。所以你可以利用乘积来欺骗,对数之和的指数:

product ( 1 .. n ) ~ exp ( sum ( ln ( 1 .. n ) ) )


提供类似的SQL (同样开始时为100,5%的年回报率) :

with rws as (
  select add_months(date'2015-01-01', ((rownum*6) - 1)) dt,
         100 price,
         1 + (0.05/2) return
  from dual connect by level <= 8
), interest as (
  select dt, 
         price * exp(sum(ln(return)) over (order by dt)) value
  from   rws
)
  select * from interest;

DT                    VALUE                                     
01-JUN-2015 00:00:00  102.499999999999999999999999999999999999  
01-DEC-2015 00:00:00  105.062499999999999999999999999999999998  
01-JUN-2016 00:00:00  107.689062499999999999999999999999999997  
01-DEC-2016 00:00:00  110.381289062499999999999999999999999995  
01-JUN-2017 00:00:00  113.140821289062499999999999999999999996  
01-DEC-2017 00:00:00  115.969341821289062499999999999999999994  
01-JUN-2018 00:00:00  118.868575366821289062499999999999999991  
01-DEC-2018 00:00:00  121.840289750991821289062499999999999991 


要计算给定时间点的付款,请使用lag()减去先前的值。并根据需要过滤结果:

with rws as (
  select add_months(date'2015-01-01', ((rownum*6) - 1)) dt,
         100 price,
         1 + (0.05/2) return
  from dual connect by level <= 8
), interest as (
  select dt, 
         price * exp(sum(ln(return)) over (order by dt)) value 
  from   rws
)
  select * from (
    select dt, round(value - lag(value) over (order by dt), 2) payments
    from   interest
  )
  where  dt between date'2016-08-01' and date'2017-09-01';

DT                    PAYMENTS  
01-DEC-2016 00:00:00  2.69      
01-JUN-2017 00:00:00  2.76  


注意:这些计算产生了很多小数!确保检查舍入问题。您需要参考基金提供者使用的计算来确定他们如何处理此问题。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论