title: How do I lag columns in MySQL?
body: <p>Consider the following table:</p>
  
  <pre><code>SELECT id, value FROM table ORDER BY id ASC;
  +-----+---------+
  | id  | value   |
  +-----+---------+
  | 12  | 158     |
  | 15  | 346     |
  | 27  | 334     |
  | 84  | 378     |
  | 85  | 546     |
  +-----+---------+
  </code></pre>
  
  <p>The <code>id</code> column is auto-incremented but contains gaps. The <code>value</code> column is numeric.</p>
  
  <p>I want to look at the increase in <code>value</code> over time by setting <code>value</code> in relation to the <code>value</code> two rows above. That is for row <code>id=85</code> I want to set the <code>value</code> of row <code>id=85</code> (546) in relation to the <code>value</code> of row <code>id=27</code> (334). The value to be computed for row <code>id=85</code> is hence 546/334=1.63473.</p>
  
  <p>This is the result I want to achieve:</p>
  
  <pre><code>SELECT id, value, ...;
  +-----+---------+---------------------+
  | id  | value   | value/lag(value, 2) | (the syntax value/lag(value, 2) is made up)
  +-----+---------+---------------------+
  | 12  | 158     | NULL                |
  | 15  | 346     | NULL                |
  | 27  | 334     | 2.11392             | (334/158=2.11392)
  | 84  | 378     | 1.09248             | (378/346=1.09248)
  | 85  | 546     | 1.63473             | (546/334=1.63473)
  +-----+---------+---------------------+
  </code></pre>
  
  <p>How do I perform such lagging in MySQL?</p>
  
  <p>Please note that the <code>id</code> column contains gaps, so simply joining on the same table with <code>t1.id = t2.id - 2</code> will not work.</p>