Say I have table and these are its sample rows
ChangeID Change
1 102
2 105
3 107
4 110
The change formula is
(CurrentRowChange - PreviousRowChange) / PreviousRowChange
Hence:
- for 1st row it should be
0 - for 2nd row it should be
(105 - 102) / 102
and so on. How can I efficiently write this formula in SQL?
I know I can write a scalar function and then do a RowNumber and order By ChangeID and fetch the row number’s Change value and then find the current row number – 1 and then fetch that row’s Change value and do a divide.
Is there any better way to achieve this?
give this a try, assuming that
CHANGEIDcan be deleted and it isIDENTITY.