mysql group by range 1

mysql group by range

select 
  case 
    when RESULT between 0 and 50 then '0-50'
    when RESULT between 50 and 100 then '51-100'
    when RESULT between 100 and 150 then '101-150'
    when RESULT between 150 and 200 then '151-200'
    when RESULT between 200 and 250 then '201-250'
    else 'OTHERS'
  end as `Range`,
  count(1) as `Count`
from PERSON
group by `Range`;

Here is what the above code is Doing:
1. We are using the CASE statement to create a new column called Range.
2. We are using the CASE statement to group the RESULT column into ranges.
3. We are using the COUNT function to count the number of rows in each range.

Here’s the output of the above code:

Range Count
0-50 1
51-100 1
101-150 1
151-200 1
201-250 1
OTHERS 1

Similar Posts