在sql中运行累积回报

Bør*_*rbo 2 sql finance return

希望获得一系列每日回报的连续累积回报?我知道这可以使用 exp 和 sum 解决,但我的回报系列不是使用 LN 计算的。

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

数据集:

在此处输入图片说明

想要的结果

在此处输入图片说明

Gor*_*off 5

这是你想要的吗?

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

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

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

我认为任何数据库都没有内置的product()聚合功能。唉。