id    type  value1    value2 
1    1    1        1 
1    2    2        2 
2    1    3        3 
2    2    4        4 把ID相同的行 value1值相加 value2值相加并更新type=2的行  并删除type=1的行 想要的结果 
id    type  value1    value2 
1    2    3        3 
2    2    7        7 

解决方案 »

  1.   

    declare @t table([id] int , type  int, value1  int,  value2 int)
    insert into @t values(1  ,  1  ,  1    ,    1 )
    insert into @t values(1  ,  2  ,  2    ,    2 )
    insert into @t values(2  ,  1  ,  3    ,    3 )
    insert into @t values(2  ,  2  ,  4    ,    4 )update  @t   set value1 = a.value1 , value2 = a.value2   from(
    select [id],sum(value1) as value1 , sum(value2) as value2 from @t group by [id] having count(*)=2) a
    inner join @t b on b.[id] = a.[id] and type =2delete from @t where [id] in( select [id] from @t group by [id] having count(*)=2) and type = 1 select * from @t 
      

  2.   

    declare @t table([id] int , type  int, value1  int,  value2 int)
    insert into @t values(1  ,  1  ,  1    ,    1 )
    insert into @t values(1  ,  2  ,  2    ,    2 )
    insert into @t values(2  ,  1  ,  3    ,    3 )
    insert into @t values(2  ,  2  ,  4    ,    4 )update  @t   set value1 = a.value1 , value2 = a.value2   from(
    select [id],sum(value1) as value1 , sum(value2) as value2 from @t group by [id] having count(*)=2) a
    inner join @t b on b.[id] = a.[id] and type =2delete from @t where [id] in( select [id] from @t group by [id] having count(*)=2) and type = 1 select * from @t
      

  3.   


    declare @t table([id] int, type int,  value1 int,   value2  int)
    insert into @t values(1 ,   1  ,  1    ,    1 )
    insert into @t values(1 ,   2  ,  2    ,    2 )
    insert into @t values(2 ,   1  ,  3    ,    3 )
    insert into @t values(2 ,   2  ,  4    ,    4 )update @t set value1 = a.value1 ,  value2 = a.value2  from(
    select [id] , 2 as type , sum(value1) as value1 , sum(value2) as value2 from @t group by [id] having Count(*)>1)a 
    inner join @t b on b.[id] = a.[id] and b.[type] =2 delete from @t where type='1' and [id] in( select [id] from @t group by [id] having Count(*)>1)select * from @t
      

  4.   

    创建临时表 或表变量insert into #t select id,2 as type,sum(value1),sum(value2) from tableName 
    where [id]=@IDdelete from tableName where [id]=@ID and type=1update tableName set value1=(select value1 from #t where id=@id),
    value2=(select value2 from #t where id=@id)
    where [id]=@ID
      

  5.   


    select id,max(type) as type , sum(value1) as value1 ,sum(value2) as value2 from table1 group by id order by id
      

  6.   

    declare @t table([id] int , type  int, value1  int,  value2 int) 
    insert into @t values(1  ,  1  ,  1    ,    1 ) 
    insert into @t values(1  ,  2  ,  2    ,    2 ) 
    insert into @t values(2  ,  1  ,  3    ,    3 ) 
    insert into @t values(2  ,  2  ,  4    ,    4 ) 
    SELECT 
    ID,
    MAX(TYPE)TYPE,
    SUM(VALUE1)VALUE1,
    SUM(value2)value2
    FROM 
    @T 
    GROUP BY 
    ID 
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)ID          TYPE        VALUE1      value2      
    ----------- ----------- ----------- ----------- 
    1           2           3           3
    2           2           7           7(所影响的行数为 2 行)