title: Counting sum of difference of values in a query
body: <p>I have a problem with an SQL query ,I have to count the difference between values in every "range" of values during a timeslot. Here is an example of what am I trying to do :</p>
  
  <p>I have a table with two column data and a timestamp</p>
  
  <pre><code>Value   Time
  5       16:00
  6       17:00
  7       18:00
  0       19:00
  1       20:00
  2       21:00
  3       22:00
  4       23:00
  5       00:00
  6       01:00
  0       02:00
  ...
  </code></pre>
  
  <p>And I have to give as result the sum of the difference of the values on every time slot. So this table will give me the result 8 because you have the difference between 7 and 5 and between 6 and 0.</p>
  
  <p>I had the idea of calculating the sum of these difference of value by analysing each row and changing the min and max value found until I reach 0 then making the difference between them and save the result in a variable that will be additioned until the end of the table using the same logic but I think it is possible to do it with one query and a quite better performance . Could you help me on that case ?</p>