I have a table as below
Value Count
value1 55
value2 60
value3 65
value4 20
value5 25
value6 45
value7 5
value8 25
I need a query which takes the difference between two successive counts and sums them up. One condition is, if the next value is less than the previous value, then the next value is considered instead of the difference. The working is as below
Value Count Diff Explanation
value1 55 No diff as there is no previous value
value2 60 5 (60-55)
value3 75 15 (75-60)
value4 20 20 (20 as 20 < 75)
value5 25 5 (25-20)
value6 45 20 (45-25)
value7 5 5 (5 as 5 < 45)
value8 25 20 (25-5)
The query should give the total of all the differences which is 5+15+20+5+20+5+20 = 90
Thank you in advance for the wonderful query you are going to craft for me :-)