title: cumulative average in MySQL
body: <p>I have a table with id and values shown below.  is it possible to get another column which takes the value divided by the cumulative average as we go down the row?</p>
  
  <pre><code>original table : t1
      +----+----------------------+
      | id | Val                  |               
      +----+---------------------+-
      |  1 |                 NULL |  
      |  2 |                  136 |    
      |  3 |                   42 |
  
  
   table i want to get 
  +----+---------------------+-----------------------------+
  | id | Val                  | VAL/(AVG(VAL) )            |              
  +----+---------------------+-----------------------------+
  |  1 |                 NULL |                    NULL    | 
  |  2 |                  136 |    136/((136+0)/2)=2.000   | 
  |  3 |                   42 |  42/((42+136+0)/3)=0.708   |
  </code></pre>
  
  <p>here is my query:</p>
  
  <pre><code>SELECT  t1.id, t1.Val, Val/AVG(t1.Val) 
  FROM    followers t1
  JOIN    followers t2
  ON      t2.id &lt;= t1.id
  group by t1.id;
  </code></pre>
  
  <p>however i get this instead:</p>
  
  <pre><code>+----+---------------------+----------------------+
  | id | Val                 | VAL/(AVG(VAL) )      |
  +----+---------------------+----------------------+
  |  1 |                NULL |   NULL               | 
  |  2 |                 136 | 1.0000               | 
  |  3 |                  42 | 1.0000               |
  </code></pre>
  
  <p>seems like AVG(Val) returns the same value from the col Val.</p>
  
  <p>I was hoping to do something similar to this link here but instead of sum i want average.
  <a href="https://stackoverflow.com/questions/5462205/mysql-select-function-to-sum-current-data">MySQL SELECT function to sum current data</a></p>
  
  <hr>
  
  <p>I re-implemented the edits and took rows with NULL into account: </p>
  
  <pre><code>+----+---------------------+---------------------+
  | id | Val                 | VAL/(AVG(VAL) )      |
  +----+---------------------+----------------------+
  |  1 |                NULL |    NULL              | 
  |  2 |                 136 |  1.0000              |&lt;---need this to = 2.000 
  |  3 |                  42 |  0.4719              |&lt;---need this to = 0.708 
  
  SELECT  t1.id, t1.Val, t1.Val/(SUM(t2.Val)/(t1.id)) AS C
  FROM    followers t1
  JOIN    followers t2
  ON      t2.id &lt;= t1.id
  group by t1.id;
  </code></pre>