字段age 的值 从1 到100,按不同年龄组分组.
如:
<10岁
11-60岁
60-90岁
>90岁计算出不同年龄组的人数

解决方案 »

  1.   

    select '<10'  age,count(*),sum(... from t where age <10               union all
    select '11-60'age,count(*),sum(... from t where age >= 11 and age< 60 union all
    select '60-90'age,count(*),sum(... from t where age between 60 and 90 union all
    select '>90'  age,count(*),sum(... from t where age >90
      

  2.   

    select '<10' age,count(*),sum(...from t where age <10          union all
    select '11-60' age, count(*),sum(...from t where age between 11 and 60 union all
    select '60-90' age, count(*),sum(...from t where age between 60 and 90 union all
    select '>90' age, count(*), sum(...from t where age >90
      

  3.   

    select xx,count(*)
    from (select (case when (age>= 0 and age<= 10) then '0-10'
                       when (age>= 3 and age< 7) then '11-20'
                        ..........
                       else '>90' end) as xx from tab 
    )
    group by xx
      

  4.   

    晕,怎么今天老写错字select xx,count(*)
    from (select (case when (age>= 0 and age<= 10) then '0-10'
                       when (age>= 11 and age<= 60) then '11-60'
                       when (age>= 61 and age<= 90) then '60-90'
                       else '>90' end) as xx from tab 
    )
    group by xx