I'm trying to figure out a way in which to calculate the following in SQL:
Where r and W is static (0.2 , 0.4) respectively. The x resembles age, and does never exceed 68.
My Database has columns for x, r and W, where x is different for each observation.
My initial thought was a while loop, but i'm not able to construct a while loop, that does something like :
select * , 0 as n
while n < 68 - (x + 1)
begin
(POWER((1 + r) / (1 + W)) , n)
set n = n + 1
end
from data
Hopefully the pseudo-code above illustrates what i am trying to implement.
Thanks in advance!
There is no need for a loop. Your sum (given r,w,x) is
(POWER( (1+r)/(1+w), 68-x)-1)/( (1+r)/(1+w) - 1)
unless
(1+r)/(1+w) == 1
in which case the sum is 68-x
For if we write
c = (1+r)/(1+w) and
N = 68-x
then
(c-1)*Sum{ 0<=n<N | pow(c,n)}
= c*Sum{ 0<=n<N | pow(c,n)} - Sum{ 0<=n<N | pow(c,n)}
= Sum{ 0<=n<N | pow( c, n+1)} - Sum{ 0<=n<N | pow(c,n)}
= Sum{ 1<=n<=N | pow( c, n)} - Sum{ 0<=n<N | pow(c,n)}
= pow( C, N) - 1 (as all the other terms cancel)