我想对一个范围内的数据进行分组汇总,比如用户年龄在21-30、31-40、41-50的做一个分组汇总,该怎么实现?

解决方案 »

  1.   

    表:
    ID ,年龄
    1,20
    2,40select ID,sum(case when 年龄>21 and年龄<30 then num ),sum(case when 年龄>31 and年龄<40 then num ),......
    from 表
    group by ID
      

  2.   

    create table #a (a int, b int)insert into #a
    select 1,20 union all
    select 2,22 union all
    select 3,28 union all
    select 4,22 union all
    select 5,30 union all
    select 6,37 union all
    select 7,34 
    select max(a),count(a) from #a group by (b-1)/10这个意思嘛?
      

  3.   

    select 年龄段,  ...
    from
    (
    select 年龄,case when 年龄 between 21 and 30 then [21-30],
                     when 年龄 between 31 and 40 then [31-40],
                     when 年龄 between 41 and 50 then [41-50] end as 年龄段,
    ...
    from tablename
    )tab
    group by 年龄段 当然也可以只写一层查询,不过看起来没有这么清晰
      

  4.   

    To:vlsmvlsm(vlsm) 
    你的是对ID进行分组,不是对一个范围的年龄进行分组汇总,是吗?
      

  5.   

    TO: ljsql(第 1 行: '脑子' 附近有语法错误。) 看不懂....
    不用给我说具体的SQL语句,只用给我说说怎么实现就OK了