如果结果中不要ID,直接这样写:
select type,sum([count]) as [count] from tbl group by type要ID就要用到临时表了
select IDENTITY(int,1,1) id, type,sum([count]) as [count] into #t from tbl group by type
select * from #t

解决方案 »

  1.   

    create table tb(id int,    type int,    count int)
    insert tb select 1      , 5,        10
    union all select 2     ,  5 ,       1
    union all select 3    ,   4  ,      10
    union all select 4   ,    4   ,     2
    union all select 5  ,     3    ,    5
    union all select 6 ,      3     ,   5select id=(select count(*) from 
    (select min(id) mi ,type,sum(count) count from tb group by type) x
    where x.mi<=b.mi)
    ,type,count
    from
    (select min(id) mi ,type,sum(count) count from tb group by type) b
    order by iddrop table tb
    /*
    id          type        count       
    ----------- ----------- ----------- 
    1           5           11
    2           4           12
    3           3           10(所影响的行数为 3 行)
    */