title: MySQL and CASE WHEN with a range of values
body: <p>I have an accounts table and a records table where accounts have multiple records. I would like to break down the account totals by "count of records" range. I.e. Show the breakdown of</p>
  
  <pre><code>Count of Records | Count
  =========================
  0-25 | 100
  25 - 50 | 122
  50 - 100 | 300
  </code></pre>
  
  <p>Etc.</p>
  
  <p>I am using the following query, but I can't get it to group by "grp" which is what I want, any help on the best way to modify query?</p>
  
  <pre><code>SELECT count(*) as ct,
      CASE 
          WHEN COUNT(*) &lt; 25 THEN '1-25'
          WHEN COUNT(*) &gt;= 25 &lt; 50 THEN '25-50'
          WHEN COUNT(*) &gt;= 50 &lt; 100 THEN '50-100'
          WHEN COUNT(*) &gt;= 100 &lt; 250 THEN '100-250'
          WHEN COUNT(*) &gt;= 250 &lt; 500 THEN '250-500'
          WHEN COUNT(*) &gt;= 500 &lt; 1000 THEN '500-1000'
          ELSE '1000+'
      END AS grp
      FROM records r,accounts a
      WHERE r.account_id=a.id
      ORDER BY ct
  </code></pre>