title: Subtract value from previous row in mysql
body: <p>I have table with the following structure</p>
  
  <pre><code>ID  VALUE
  1   100
  2   200
  3   300
  4   400
  5   500
  </code></pre>
  
  <p>I want output like this</p>
  
  <pre><code>ID  VALUE  DIFF_to_Prev
  1   100     0
  2   200    100 
  3   300    100 
  4   400    100
  5   500    100
  </code></pre>
  
  <p>This is the query I've tried so far</p>
  
  <pre><code>SET @LastVALUE:= 0;
  SET @LastSN:= 0;
  
  SELECT dtr.SN, dtr.VALUE, 
         IF(@LastSN = dtr.SN, dtr.Value - @LastVALUE, 0) DIFF_to_Prev     
      FROM difftworows as dtr
  </code></pre>
  
  <p>This is the results I get from it:</p>
  
  <pre><code>ID      VALUE   DIFF_to_Prev
  1       100     0
  2       200     0
  3       300     0
  4       400     0
  5       500     0
  </code></pre>
  
  <p>I want to know what I'm doing wrong. Please tell me how can I fix it with suggestions.</p>
  
  <p>Thanks!!</p>