title: mysql moving average of N rows
body: <p>I have a simple MySQL table like below, used to compute MPG for a car.  </p>
  
  <pre><code>+-------------+-------+---------+
  | DATE        | MILES | GALLONS |
  +-------------+-------+---------+
  | JAN 25 1993 |  20.0 |    3.00 |
  | FEB 07 1993 |  55.2 |    7.22 |
  | MAR 11 1993 |  44.1 |    6.28 |
  +-------------+-------+---------+
  </code></pre>
  
  <p>I can easily compute the Miles Per Gallon (MPG) for the car using a select statement, but because the MPG varies widely from fillup to fillup (i.e. you don't fill the exact same amount of gas each time), I would like to computer a 'MOVING AVERAGE' as well.  So for any row the MPG is MILES/GALLON for that row, and the MOVINGMPG is the SUM(MILES)/SUM(GALLONS) for the last N rows.  If less than N rows exist by that point, just SUM(MILES)/SUM(GALLONS) up to that point.</p>
  
  <p>Is there a single SELECT statement that will fetch the rows with MPG and MOVINGMPG by substituting N into the select statement?</p>