title: MySQL- Average of 15 minutes intervals
body: <p>I have the following table:</p>
  
  <pre><code>╔════════════╦══════════╦═══════╦══════╗
  ║    DATE    ║   TIME   ║ Price ║ Avg  ║
  ╠════════════╬══════════╬═══════╬══════╣
  ║ 01/01/2000 ║ 00:00:00 ║   1   ║      ║
  ║ 01/01/2000 ║ 00:05:00 ║   2   ║      ║
  ║ 01/01/2000 ║ 00:10:00 ║   3   ║      ║
  ║ 01/01/2000 ║ 00:15:00 ║   4   ║      ║
  ║ 01/01/2000 ║ 00:20:00 ║   5   ║      ║
  ║ 01/01/2000 ║ 00:25:00 ║   6   ║      ║
  ║ 01/01/2000 ║ 00:30:00 ║   7   ║      ║
  ║ 01/01/2000 ║ 00:35:00 ║   8   ║      ║
  ║ 01/01/2000 ║ 00:40:00 ║   9   ║      ║
  ║ 01/01/2000 ║ 00:45:00 ║   10  ║      ║
  ║ 01/01/2000 ║ 00:50:00 ║   11  ║      ║
  ║ 01/01/2000 ║ 00:55:00 ║   12  ║      ║
  ║ 01/01/2000 ║ 01:00:00 ║   13  ║      ║
  ╚════════════╩══════════╩═══════╩══════╝
  </code></pre>
  
  <p>And I want to fill the column Avg with a moving average of price of the last 15 minutes (not including the last average), so that the outcome is:</p>
  
  <pre><code>╔════╦════════════╦══════════╦═══════╦══════╗
  ║ ID ║    DATE    ║   TIME   ║ Price ║ Avg  ║
  ╠════╬════════════╬══════════╬═══════╬══════╣
  ║ 1  ║ 01/01/2000 ║ 00:00:00 ║   10  ║  10  ║
  ║ 2  ║ 01/01/2000 ║ 00:05:00 ║   2   ║      ║
  ║ 3  ║ 01/01/2000 ║ 00:10:00 ║   6   ║      ║
  ║ 4  ║ 01/01/2000 ║ 00:15:00 ║   4   ║  4   ║ &lt;-- Average of 2,6,4
  ║ 5  ║ 01/01/2000 ║ 00:20:00 ║   7   ║      ║
  ║ 6  ║ 01/01/2000 ║ 00:25:00 ║   6   ║      ║
  ║ 7  ║ 01/01/2000 ║ 00:30:00 ║   5   ║  6   ║ &lt;-- Average of 7,6,5
  ║ 8  ║ 01/01/2000 ║ 00:35:00 ║   2   ║      ║
  ║ 9  ║ 01/01/2000 ║ 00:40:00 ║   2   ║      ║
  ║ 10 ║ 01/01/2000 ║ 00:45:00 ║   2   ║  2   ║ &lt;-- Average of 2,2,2
  ║ 11 ║ 01/01/2000 ║ 00:50:00 ║   10  ║      ║
  ║ 12 ║ 01/01/2000 ║ 00:55:00 ║   12  ║      ║
  ║ 13 ║ 01/01/2000 ║ 01:00:00 ║   2   ║  8   ║ &lt;-- Average of 10,12,2
  ╚════╩════════════╩══════════╩═══════╩══════╝
  </code></pre>
  
  <p>NOTE: While there is ALWAYS an entry every 5 minutes, there could be additional entries in between(i.e.: There could be a price at 00:32:00).</p>
  
  <p>What I have right now is the following:</p>
  
  <pre><code>UPDATE table AS t 
  INNER JOIN 
  (SELECT AVG(Price) as average,
      floor((unix_timestamp(CONCAT(date, ' ', time)))/900 ) as timekey
  FROM table
  GROUP BY timekey ) t1 
  ON t.ID = t1.ID
  SET 15_MIN_AVERAGE = t1.average
  </code></pre>
  
  <p>But this seems to group the entries that are on the 15 minutes and the next ones, instead of the previous ones like I want.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>Thanks!</p>