on ‎2011 Sep 07 9:26 PM
I have a table with 2 columns(name and value). The values can range from -5000 to +5000. How do I write a query to get the count of values in ranges like
Range Count
___
___
0-10 4
10-20 8
20-30 400
Request clarification before answering.
How about just doing a query for each range and UNIONing the result sets?
SELECT '0 - 10', COUNT() FROM mytable WHERE mytable.value BETWEEN 0 AND 10 UNION SELECT '11 - 20', COUNT() FROM mytable WHERE mytable.value BETWEEN 11 AND 20 UNION (and so on...)
Of course, this simple approach will only be realistic if you have a finite, and relatively small, number of ranges you want to report.
Another thought would be to write a procedure that loops through ranges that increment each pass, essentially just automating my simple-minded UNION idea.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 15 | |
| 9 | |
| 6 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.