Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL group integer column into groups of 100

I have a MySQL table and I am looking to create a stats page on the data.

Id (integer), Pri (integer), blab1, blab2, blab3, ect...

The "pri" field has a number between 0-1000. I would like to how the rows are spread across the pri in the table. I am looking for a way to group the "pri" by 100s and count the number of rows in each group.

For example:

Range   | Count
-----------------
  0- 99 |  999
100-199 |   50 
200-299 | 3587 

The easier way would be to run 10 separate queries for each range, (WHERE pri >= 400 AND pri < 500) but I was wondering if there was a easier way?

like image 535
Steven Smethurst Avatar asked Sep 05 '25 03:09

Steven Smethurst


1 Answers

You can always trunc the pri column to like so:

SELECT 
    truncate(pri/100,0)*100 AS Range_Start,
    count(*)
FROM ...
GROUP BY
    truncate(pri/100,0)*100;
like image 192
a'r Avatar answered Sep 07 '25 23:09

a'r