title: Count amount of records per account group, where joined date = each month
body: <p>I have a table (accounts) that holds the accountgroup and joineddate of accounts, amongst other fields. Using MySQL version 5.5.35-cll. </p>
  
  <p>Essentially what I'm trying to do is count the amount of 'registrations' that happen each month, per accountgroup. (Not just the total amount per month - need to know how many registrations are occurring in each accountgroup, each month).  </p>
  
  <p>So the result would look something like this:</p>
  
  <p><a href="http://imageshack.com/a/img842/9105/drh9.gif" rel="nofollow noreferrer">intended results http://imageshack.com/a/img842/9105/drh9.gif</a></p>
  
  <p>I've tried a few different approaches, but have only been successful getting what I need for one specified month, as opposed to all months of the year.  When I tried to use SUM it looked like it was working, but it didn't work as expected - would seem like it thought there was a maximum of 1 registrations per month per group, when in reality there are at least 4 or 5 for some groups.</p>
  
  <p>This is my most recent (failing) attempt with SUM: </p>
  
  <pre><code>SELECT accountgroup, 
  SUM( IF( MONTH =1, numRecords, 0 ) AND YEAR=2014) AS  'January', 
  SUM( IF( MONTH =2, numRecords, 0 ) AND YEAR=2014) AS  'February', 
  SUM( IF( MONTH =3, numRecords, 0 ) AND YEAR=2014) AS  'March', 
  SUM( IF( MONTH =4, numRecords, 0 ) AND YEAR=2014) AS  'April', 
  SUM( IF( MONTH =5, numRecords, 0 ) AND YEAR=2014) AS  'May', 
  SUM( IF( MONTH =6, numRecords, 0 ) AND YEAR=2014) AS  'June', 
  SUM( IF( MONTH =7, numRecords, 0 ) AND YEAR=2014) AS  'July', 
  SUM( IF( MONTH =8, numRecords, 0 ) AND YEAR=2014) AS  'August', 
  SUM( IF( MONTH =9, numRecords, 0 ) AND YEAR=2014) AS  'September', 
  SUM( IF( MONTH =10, numRecords, 0 ) AND YEAR=2014) AS  'October', 
  SUM( IF( MONTH =11, numRecords, 0 ) AND YEAR=2014) AS  'November', 
  SUM( IF( MONTH =12, numRecords, 0 ) AND YEAR=2014) AS  'December', 
  SUM( numRecords ) AS total
  FROM (
  SELECT accountgroup, MONTH( joineddate ) AS MONTH, YEAR( joineddate ) AS YEAR, COUNT( * ) AS numRecords
  FROM accounts 
  WHERE accountgroup &lt;&gt;''
  GROUP BY accountgroup, MONTH, YEAR) AS SubTable1
  GROUP BY accountgroup");
  </code></pre>
  
  <p><strong>Anyone able to show me how to do this? I'm pretty stumped :(<br>
  Thanks in advance for any help.</strong> </p>