I have a table called Person that contain a field called PersonAge. I need to group the ages by age bands ie ’12 and under’, ’13-17′, ’18-25′, ’25 and over’ and return this resultset using a stored procedure.
Ideally I need to get returned 2 fields , ‘Age Band’, ‘Total’ like so
Age band Total 12 and under 5 13 - 17 8 18 - 25 7 25 and over 10
Create a table containing your bands:
Then populate it with your data:
Then join with it:
This allows for flexibility in adjusting the bands. Of course, the other answer here using UNION is usable too, which is more appropriate if you can/won’t add another table to your database.