title: MySQL: Group by range of values (From, To)
body: <p>I'm looking for a way to group results by ranges. </p>
  
  <p>For example:</p>
  
  <p><strong>Data:</strong></p>
  
  <pre><code>ID | Value
  1  |     5
  2  |    10 
  3  |    30
  4  |    44
  5  |    71 
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre><code>|From  | To  | Count(*) |
  |    0 |  20 |        2 |
  |   20 |  40 |        1 |
  |   40 |  60 |        0 |
  |   60 |  80 |        1 |
  </code></pre>
  
  <p>I know I could easily do this with a case or where query however I have several tables with an enormous variation in ranges (which also include decimal values) so I was hoping for a solution which would automatically create ranges of 20 in between the minimum and maximum values.</p>
  
  <p>Two solutions which I have found but were unable to adapt:</p>
  
  <p><a href="https://stackoverflow.com/questions/1999421/group-by-price-range">group by price range</a> (Very similar but it creates ranges from values so there will never be 0 ranges and doesn't do whole numbers)</p>
  
  <p><a href="https://stackoverflow.com/questions/13779777/group-rows-by-year-band-interval">Group rows by Year Band Interval</a> (I'm not familiar with the use of floor but this is what I'm trying to accomplish, but I couldn't get it to accurately count the rows)</p>
  
  <p>Thank you so much for your help</p>
  
  <p>James</p>