sql ibm db2 group by age range

时间:2016-12-26 20:12:08

标签: sql group-by db2

我有一张表INFO,其年龄参数为AGE_YEARS。 我需要这样的输出:

AGE_RANGE    COUNT
18-29        400 
30-49        500
50-69        ???
70-99        ???

select语句对此有何看法?

1 个答案:

答案 0 :(得分:0)

您可以使用case

select (case when age_years >= 18 and age < 30 then '18-29'
             when age_years < 50 then '30-49'
             when age_years < 70 then '50-69'
             when age_years < 100 then '70-100'
        end) as age_range, count(*)
from REDACTED
group by (case when age_years >= 18 and age < 30 then '18-29'
               when age_years < 50 then '30-49'
               when age_years < 70 then '50-69'
               when age_years < 100 then '70-100'
          end)
order by min(age_years);