Case when statement and looping?
===

So I've been told that looping is not a desirable thing to do in SQL.  The following question makes a histogram based on age ranges. As you can see, all of the groups are hardcoded.  In this instance all of the values for which I'm looking are under 100.  What to do if these ranged into the thousands?  How could this be made more extensible?


    select bins, count(*) as numbers from
    (
    
    select id, patientage,
    case 
    	when patientage between 20 and 29 then '20-29' 
    	when patientage between 30 and 39 then '30-39'
    	when patientage between 40 and 49 then '40-49'
    	when patientage between 50 and 59 then '50-59'
    	when patientage between 60 and 69 then '60-69'
    	when patientage between 70 and 79 then '70-79'
    	when patientage between 80 and 89 then '80-89'
    	when patientage between 90 and 99 then '90-99'
    
    
    end as bins
    from patient
    inner join tblhospitals on tblhospitals.hospitalnpi=patient.hospitalnpi
    where (tblhospitals.hospitalname like '%university%')
    ) as t
    group by bins
    order by bins

