declare @table1 table (type int,price int)
insert into @table1 values (2,232)
insert into @table1 values (2,34)
insert into @table1 values (3,676)
insert into @table1 values (4,67)
insert into @table1 values (5,345)
insert into @table1 values (6,345)select case when type between 2 and 3 then '2、3' when type between 4 and 5 then '4、5' else cast(type as varchar) end as type,sum(price)sum from @table1 group by case when type between 2 and 3 then '2、3' when type between 4 and 5 then '4、5' else cast(type as varchar) end

解决方案 »

  1.   

    谢谢 DJMPH(冷开水) ,但是如果我其余的想分成一组,该怎么写呢?
    谢谢
      

  2.   

    declare @table1 table (type int,price int)
    insert into @table1 values (2,232)
    insert into @table1 values (2,34)
    insert into @table1 values (3,676)
    insert into @table1 values (4,67)
    insert into @table1 values (5,345)
    insert into @table1 values (6,345)
    insert into @table1 values (7,232)
    insert into @table1 values (8,232)
    select case when type between 2 and 3 then '2、3' when type between 4 and 5 then '4、5' else 'others' end as type,sum(price)sum from @table1 group by case when type between 2 and 3 then '2、3' when type between 4 and 5 then '4、5' else 'others'  end
      

  3.   

    楼上的怎么总是这句啊,yinzhen(銀圳) 和DJMPH(冷开水) 都可以啊,不用再找答案了吧
      

  4.   

    select case type when 2 then 2 when 3 then 2 when 4 then 4 when 5 then 4 else type end,sum(price) from 
    table1 
    group by case type when 2 then 2 when 3 then 2 when 4 then 4 when 5 then 4 else type end