I have a table without identity field. the structure about:
Date date,
SomeFK int,
Key float,
Value float
I need to be able to select not using cursor something like
select Key, PrevKey, Value from ... where Key > @a and Key <= @b
where the PrevKey has to be a Key from previous record, given that the recordset ordered by Key
If data like this:
0.1, 2.0
0.2, 3.0
0.3, 5.0
0.4, 4.0
Then expected result for @a=0.15 and @b=0.3
0.2, 0.1, 3.0
0.3, 0.2, 5.0
What I am trying to do is to calculate a formula: SUM(Value[i] * (Key[i] – Key[i-1]))
One way you could try is this CTE approach:
Edit
Just seen that your PK is not exclusively on “Key”, which the above example assumes.