title: mysql aggregate multiple columns by hour
body: <p>Had a quick look through and couldn't find my question again. So I'm hoping someone here can help.
  I have a large table with a similar structure to the below.</p>
  
  <p><code>DateTime | InboundUserID | OutboundUserID | CustomerUserID | Cost | Mins | Account Number</code></p>
  
  <p>I'm trying to group the above data into hourly chunks and store the result in another table with a similar structure to below.</p>
  
  <p><code>Date | HourFrom | HourTo | SumMins | SumCost | OutboundUserID | CustomerUserID | Account Number</code></p>
  
  <p>I've written the following code / query in order to do this but it's not quite right. Basically the group by means not all the data gets summarised. For instance when looking at the newly created table I have all the OutboundUserID data, but none of the CustomerUserID data.</p>
  
  <pre><code>    for(int i=0;i &lt;=23;i++)
      {
          for(int j=1 ; j &lt;=31 ; j++)
          {
              String Time = String.format("%02d", i);
  
              query = "INSERT DELAYED INTO correlated_calls_hourly " +
                      " SELECT DateOnly,HourFrom,HourTo,DURATION,CALLCOUNT,`Price Group ID`,INBOUNDCOST,INBOUNDREVENUE,`InboundCustomerID`,OUTBOUNDCOST,OUTBOUNDREVENUE,`Outbound Customer ID`,`Customer ID`,`Account Number`,`Service` FROM" +
                      " ("+
                          " SELECT "+
                          /* snip sum(),case and date/time part of query */
                          " GROUP BY  DateOnly,`InboundCustomerID`,`OutboundCustomerID`,`CustomerID`,`AccountNumber`,`PriceGroupID`,`Service`" +
                      " )a ";
          }
      }
  </code></pre>
  
  <p>I've already taken the step of creating three different tables to summarise the data separately by OutboundUserID/ InboundUserID / CustomerUserID. But it would be a lot more elegant if I could just get this one summarised table working correctly.</p>
  
  <p>If anyone could help me think of a way to get the desired result via a single sql query I'd be greatly appreciative. </p>
  
  <p>Many thanks in advance</p>
  
  <p>Alan</p>