title: Combining SUM and AVG with GROUP BY in MySQL
body: <p>I'm not sure if this is going to be possible in a single query, but I'm trying to achieve the following with MySQL and haven't yet found a solution.</p>
  
  <p>I have a table that is structured a little like this:</p>
  
  <pre><code> ID     LOG_VALUE   LOG_TIME     
  </code></pre>
  
  <hr>
  
  <pre><code>  1        5000     19:05:42   
    2        6205     19:05:45
    3        1000     19:05:40  
    1        5000     19:05:52    
    2        6403     19:05:55   
    3        1500     19:05:50
    1        5000     19:06:02    
    2        6403     19:06:05   
    3        1500     19:06:00
  </code></pre>
  
  <p>Rows are inserted into this log table at regular intervals i.e. every 10 seconds. I would like to query the records to calculate a combined average log_value for every minute.</p>
  
  <p>The problem that I have is that I'm not sure how to calculate an average per ID for each minute, along with the sum of the averages for any given minute, only one or the other.</p>
  
  <p>In basic terms I am wanting to achieve this process:</p>
  
  <ul>
  <li>Find the average of all logs for ID 1 in 19:05</li>
  <li>Find the average of all logs for ID 2 in 19:05</li>
  <li>Find the average of all logs for ID 3 in 19:05</li>
  <li>Add the averages together to give a total for 19:05</li>
  <li>Do the same for 19:06</li>
  </ul>
  
  <p>The SQL below doesn't do what I want but I've included it to help support the problem.</p>
  
  <pre><code>SELECT ROUND(AVG(log_value)) AS average_value, EXTRACT(HOUR_MINUTE FROM log_time)
  FROM (`logs`)
  GROUP BY MINUTE(log_time), id
  ORDER BY log_time DESC
  </code></pre>
  
  <p>Thanks for any help you can provide.</p>