type inum
1 2
2 3
3 5
1 4
2 8
3 11select count(*) from 表 group by typetype count
1 6
2 11
3 16现在想要把类型1,2的合并count
type count
1和2 17
3 16该如何写

解决方案 »

  1.   

    create table tb (type int ,num int)
    go
    insert tb 
    select 1, 2 union all
    select 2 ,3 union all
    select 3 ,5 union all
    select 1 ,4 union all
    select 2 ,8 union all
    select 3 ,11select type,SUM(num)num from (
    select case TYPE when 2 then 1 when 1 then 1 else 3 end type,num from tb)a group by type
    /*
    type num
    1 17
    3 16
    */
      

  2.   

    select sum(inum) from tt group by (type-1)/2
      

  3.   

    select type,SUM(num)num from (
    select case TYPE when 2 then '1'+'和2' when 1 then '1'+'和2' else '3' end type,num from tb)a group by type
    /*
    type num
    1和2 17
    3 16
    */
      

  4.   

    select type ,sum(inum) from 表名   group by type
      

  5.   


    use tempdb 
    go
    create table tb (type int ,num int)
    go
    insert tb 
    select 1, 2 union all
    select 2 ,3 union all
    select 3 ,5 union all
    select 1 ,4 union all
    select 2 ,8 union all
    select 3 ,11select type ,sum(num) as num from (select case when type=1 then '1和2' when type=2 then '1和2' else '3' end type, sum(num) as num  from tb group by type)a group by typetype num
    ---- -----------
    1和2  17
    3    16(2 行受影响)