开发者

Sql = how to calculate the return vector with a time series of prices?

开发者 https://www.devze.com 2022-12-14 18:09 出处:网络
I have a table which has columns of price and date, ordered by the ascending dates. I need to calculate from this a return vector where return = price ( i) / price ( i- 1). The time is not开发者_JAVA技

I have a table which has columns of price and date, ordered by the ascending dates. I need to calculate from this a return vector where return = price ( i) / price ( i- 1). The time is not开发者_JAVA技巧 time based, which means that one record can be at 9h34, the next at 9h35, then 9h40 etc...

I have found the following topic: SQL Syntax for calculating results of returned data but in Oracle I can't use order by in a subquery, could you please help me?


In Oracle, you could use the lag analytic function:

select 
     price / (lag(price) over (order by i))
,    ...
from PriceHistory

Here, lag(price) over (order by i) returns the price of the previous row, in a set ordered by the i column.

0

精彩评论

暂无评论...
验证码 换一张
取 消