Skip to content
Advertisement

SQL Data range using Case

I have table1 with data

Age
10
21
35
50

and my query

select count(*) as Total, *
from 

( 
select
case

 when age <18 then '0-20'
 when age between 20 and 29 then '20-29'
 when age between 30 and 39 then '30-39'
 when age between 40 and 49 then '40-49'
 when age between 50 and 59 then '50-59'
 when age between 30 and 39 then '30-39'
 when age between 60 and 99 then '60+'
 end as age_range
 from table1
 ) t
 group by t.age_range

The result

Total   age_range
1       0-20
1       20-29
1       30-39
1       50-59

How do I want to see the result like this ( missing 40-49 with count 0 and above 60+ count 0)

Total   age_range
1        0-20
1        20-29
1        30-39
0        40-49
1        50-59
0        60+

Thank you for your help.

Advertisement

Answer

You can enumerate the ranges with row_constructor values(), then bring the table with a left join, and finally aggregate:

select count(t.age) total, r.age_range
from (values 
    ( '0-19',  0, 19),
    ('20-29', 20, 29),
    ('30-39', 30, 39),
    ('40-49', 40, 49),
    ('50-59', 50, 59),
    (  '60+', 60, 99),
) r(age_range, low, high)
left join table1 t
    on t.age between r.low and r.high
group by r.age_range
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement