title: SQL — segment by groups
body: <p>In a MySQL DB, I have a purchases table that has these columns:</p>
  
  <pre><code>USERID PURCHASE_AMOUNT  
  3         20
  9         30
  3         5
  4         5
  1         10
  1         5
  </code></pre>
  
  <p>I would like to generate a report like this</p>
  
  <pre><code>SUM_OF_PURCHASES_RANGE NUM_OF_USERS  
  0-1                    0
  1-5                    1
  5-20                   1
  20-30                  2
  </code></pre>
  
  <p>Where it means: there are 0 users who bought up to 1(SUM of purchases) (inclusive), there are 1 users who bought between 1 to 5 etc...</p>
  
  <p>What query should I use to generate it?</p>