caliph
caliph

Reputation: 1439

calculate variance using SQL

I have a financial price time series:

date     price   variance_3

1/1/2012  1,1    n/a
2/1/2012  1,2    n/a
3/1/2012  1,3    
4/1/2012  1,3
7/1/2012  1,2
8/1/2012  1.3

I intend to calculate the variance for each date using the last 3 prices in time. Do you see a chance to accomplish this using SQL only?

Any hint would be much appreciated.

Upvotes: 5

Views: 5010

Answers (1)

Andomar
Andomar

Reputation: 238176

select  var_pop(price) as variance
from    (
        select  price
        from    YourTable
        order by
                date desc
        limit   3
        ) as SubQueryAlias

Upvotes: 2

Related Questions