group by 统计时,有些值要求合并在一起统计,
比如 
group by ageage字段出现的值为{10,20,30,40,50,60,70}
但要求10,20,30放在一起统计(该组取名为“年轻”),
40,50放在一起统计(该组取名为“中年”),
其余取名为“老年”,如何实现?

解决方案 »

  1.   

    看看zj老大的这个..--测试数据
    DECLARE @t TABLE(ID int PRIMARY KEY,col decimal(10,2))
    INSERT @t SELECT 1 ,26.21
    UNION ALL SELECT 2 ,88.19
    UNION ALL SELECT 3 , 4.21
    UNION ALL SELECT 4 ,76.58
    UNION ALL SELECT 5 ,58.06
    UNION ALL SELECT 6 ,53.01
    UNION ALL SELECT 7 ,18.55
    UNION ALL SELECT 8 ,84.90
    UNION ALL SELECT 9 ,95.60--统计
    SELECT a.Description,
    Record_count=COUNT(b.ID),
    [Percent]=CASE 
    WHEN Counts=0 THEN '0.00%'
    ELSE CAST(CAST(
    COUNT(b.ID)*100./c.Counts
    as decimal(10,2)) as varchar)+'%'
    END
    FROM(
    SELECT sid=1,a=NULL,b=30  ,Description='<30' UNION ALL
    SELECT sid=2,a=30  ,b=60  ,Description='>=30 and <60' UNION ALL
    SELECT sid=3,a=60  ,b=75  ,Description='>=60 and <75' UNION ALL
    SELECT sid=4,a=75  ,b=95  ,Description='>=75 and <95' UNION ALL
    SELECT sid=5,a=95  ,b=NULL,Description='>=95' 
    )a LEFT JOIN @t b 
    ON (b.col<a.b OR a.b IS NULL)
    AND(b.col>=a.a OR a.a IS NULL)
    CROSS JOIN(
    SELECT COUNTS=COUNT(*) FROM @t
    )c
    GROUP BY a.Description,a.sid,c.COUNTS
    ORDER BY a.sid
    /*--结果:
    Description    Record_count  Percent
    ------------------- ------------------ ---------------------- 
    <30          3            33.33%
    >=30 and <60  2            22.22%
    >=60 and <75  0            0.00%
    >=75 and <95  3            33.33%
    >=95         1            11.11%
    --*/
      

  2.   

    SELECT CASE WHEN age>=10 AND age<40 THEN '年龄'
               WHEN age>=40 AND age<70 THEN '中年'
               ELSE age>=70 THEN '老年'
           END,
           COUNT(*) AS 人数
    FROM tb_name
    GROUP BY 
      CASE WHEN age>=10 AND age<40 THEN '年龄'
           WHEN age>=40 AND age<70 THEN '中年'
           ELSE age>=70 THEN '老年'
      END
      

  3.   

    不用Group by,
    select age as 年轻 from sttable where age in(10,20,30)
      

  4.   

    --写错了个elseSELECT CASE WHEN age>=10 AND age<40 THEN '年龄'
               WHEN age>=40 AND age<70 THEN '中年'
               ELSE '老年'
           END,
           COUNT(*) AS 人数
    FROM @t
    GROUP BY 
      CASE WHEN age>=10 AND age<40 THEN '年龄'
           WHEN age>=40 AND age<70 THEN '中年'
           ELSE '老年'
      END
      

  5.   

    select age,case when age<=30 then '轻年' when age>30 and age<=50 then '中年' else '老年' end as 类型 from tb
      

  6.   

    Group by不就是分组汇总的意思
      

  7.   


    SELECT CASE WHEN age in (10,20,30) THEN '年轻'
               WHEN age in (40,50) THEN '中年'
               ELSE '老年'
           END,
           COUNT(*) AS 人数
    FROM tb_name
    GROUP BY 
      CASE WHEN age in (10,20,30) THEN '年轻'
           WHEN age in (40,50) THEN '中年'
           ELSE '老年'
      END
      

  8.   

    先在表table中加一列名为'年龄组',然后统计.
    alert table add 年龄组 varchar(20)
    update table set 年龄组='青年' where age<=30
    update table set 年龄组='老年' where age >30
    select 年龄组,sum(age) from table where 年龄组='青年' group by 年龄组 union select 年龄组,sum(age) from table where 年龄组='老年' group by 年龄组
      

  9.   

    少写了一组
    先在表table中加一列名为'年龄组',然后统计. 
    alert table add 年龄组 varchar(20) 
    update table set 年龄组='青年' where age <=30 
    update table set 年龄组='青年' where age>30 and age <=50
    update table set 年龄组='老年' where age >50 
    select 年龄组,sum(age) from table where 年龄组='少年' group by 年龄组 union select 年龄组,sum(age) from table where 年龄组='青年' group by 年龄组 union select 年龄组,sum(age) from table where 年龄组='老年' group by 年龄组
      

  10.   

    select (select count(*) from '表名' where age<30) '年轻',(select count(*) from '表名' where age>30 and age<50) as '中年',(select count(*) from '表名' where age>50) as '老年'from '表名'
      

  11.   

    --2select identity(int,1,1)num,* into #t from aselect  (case when age between 10 and 30 then 'aa'
                         when age between 40 and 50 then 'bb'
                          else 'cc' end),count(*),count(1)/(select cast ( count(*) as float )from a )
    from  a group by (case when age between 10 and 30 then 'aa'
                         when age between 40 and 50 then 'bb'
                          else 'cc' end)
      

  12.   

    select count(*) as 青年人数 as from tableName where age >=10 and age <=30 
    union 
    select count(*) as 中年人数 from tableName where age >=40 and age <=50 
    union 
    select count(*) as 老年人数 from tableName where age not age >=60
      

  13.   

    select count(*) as 青年人数 as from tableName where age >=10 and age <=30 
    union 
    select count(*) as 中年人数 from tableName where age >=40 and age <=50 
    union 
    select count(*) as 老年人数 from tableName where age >=60