在sql中运行累计回报

问题描述:

寻求一系列日收益的运行累积回报?我知道这可以使用exp和sum来解决,但是我的返回序列不是使用LN计算的。在sql中运行累计回报

希望解决这个问题而不使用循环,因为它们在sql中效率非常低。 使其快速运行非常重要。

数据集:

enter image description here

期望的结果

enter image description here

+0

您现在有机会解决您的问题,以便获得有用的信息。示例数据,期望的结果和您尝试的SQL都可以更好地提出问题。 – 2014-09-06 13:30:17

这是你想要的吗?

select t.*, 
     (select exp(sum(log(1 + return))) - 1 
     from table t2 
     where t2.date <= t.date 
     ) as cumereturn 
from table t; 

exp()log()的功能可能是您正在使用的数据库不同。在许多数据库中,你也可以使用:

select t.*, exp(sum(log(1 + return) over (order by date)) - 1 
from table t; 

我不认为任何数据库在product()聚集功能的建立。唉。

转换(1+r)(1+r)(1+r)

exp(log(1+r) + log(1+r) + log(1+r)) 

不起作用,因为1个+ R可能是负的。 负号日志未定义。即回报率小于-100%

+0

你会如何处理负面? – 2017-09-08 03:48:57