My question is a little tricky and I would appreciate if anyone help me.
I have the following table which I want to calculate the last column (intellectual capital prior to current competition) based on the scores users received in the previous competitions. the scores decays over time by the following formula:
score*e^(-t/500)
t is the number of days that have passed from the prior competition. if the user have participated in more than one competition prior to the current one we add the scores.
the following table illustrates what I want to calculate.
competitionsId UserId date score intellectual-capital-prior-to-current
1 100 1/1/2015 3000
1 200 1/1/2015 3000
1 300 1/1/2015 3000
1 400 1/1/2015 3000
2 100 1/5/2015 4000 3000* POWER(e, -4/500)
2 400 1/5/2015 4000 3000* POWER(e, -4/500)
3 100 1/10/2015 1200 3000* POWER(e,-9/500)+ 4000*POWER(e,-5/500)
3 300 1/10/2015 1200 3000*POWER(e,-9/500)
3 400 1/10/2015 1200 3000* POWER(e, -9/500) + 4000*POWER(e,-5/500)
4 200 1/20/2015 1000 3000*POWER(e,-19/500)
4 300 1/20/2015 1000 3000*POWER(e,-19/500)+ 1200*POWER(e,-10/500)
for example prior to competition 3, user100 has participated in competition 2 and competition 1. her score in competition 1 is 3000 so considering decaying factor we have 3000*e^(-9/500) and her score in competition 2 is 4000 so considering decaying factor we have 4000*e^(-5/500). Therefore user100 intellectual-capital in competition 3 is: 3000*e^(-9/500) + 4000*e^(-5/500)
The following may help you arrive at the wanted calculation. I wasn't entirely sure what
e
represents in your formula, but with some window functions we can get the needed previous values and also accumulate values.DEMO at SQL Fiddle (MS SQL Server 2014 Schema Setup)
Query 1:
Results: