Friday, June 27, 2014

Rolling Calculation

I'm using SQL Server 2012. I need to calculate the values for the result column in the table below:




The result of the 1st row is already known. The result of the next row is (result of previous row)*(1+perf), e.g.


result of row 2 = (result of row 1)*(1+perf of row 2)


result of row 3 = (result of row 2)*(1+perf of row 3)


This calculation can be done easily and fast in Excel, but I need to do it in a T-SQL function. I tried to use CURSOR and WHILE loop, but both are very slow for large number of rows.


Anybody can tell me how to write a query that can run fast?





No comments:

Post a Comment