现在我有一张表A,有字段a,b,c
现在我要做得是把c = a +2
有个前提条件是b必须相等,
因为表A是先按照b,再按照a排列的
例如:
a    b     c
-------------------
1    1     
2    1
3    1     
4    2   
5    3  
结果应该为:
a    b     c
-------------------
1    1     3
2    1     4
3    1     5
4    2     null
5    3     null

解决方案 »

  1.   

    update t set c=case when exists (select 1 from a where b=t.b and a<>t.a) then a+2 else null end
    from a t
      

  2.   

    --> 测试数据: @s
    declare @s table (a int,b int,c int)
    insert into @s
    select 1,1,null union all
    select 2,1,null union all
    select 3,1,null union all
    select 4,2,null union all
    select 5,3,nullselect a,b,c=case when exists(select 1 from @s where b=a.b and a!=a.a) then a+2 else null end from @s a 
    --结果 
    a           b           c           
    ----------- ----------- ----------- 
    1           1           3
    2           1           4
    3           1           5
    4           2           NULL
    5           3           NULL
      

  3.   

    declare @tb table(a int,b int,c int)
    insert @tb select 1,1,null    
    union all select 2,1,null   
    union all select 3,1,null    
    union all select 4,2,null  
    union all select 5,3,null update @tb set c=a+2 from @tb a where exists(select 1 from @tb where b=a.b and a<>a.a)
    select * from @tb/*
    a           b           c           
    ----------- ----------- ----------- 
    1           1           3
    2           1           4
    3           1           5
    4           2           NULL
    5           3           NULL(所影响的行数为 5 行)*/
      

  4.   

    a    b    c 
    ------------------- 
    1    1    
    2    1 
    3    1    
    4    2  
    5    3  
    结果应该为: 
    a    b    c 
    ------------------- 
    1    1    3 
    2    1    4 
    3    1    5 
    4    2    null 
    5    3    null select a,b,c=a+2 from tb t where exists(select 1 from tb where b = t.b and a <> t.a)
      

  5.   


    create table #t(a int,    b int, c int)
    insert into #t 
    select 1,    1  ,null  union all
    select 2,    1  ,null  union all
    select 3,    1  ,null  union all
    select 4,    2  ,null  union all 
    select 5,    3  ,nullupdate #t 
    set c=a+2
    from #t a
    where exists(select 1 from #t where a.b=b group by b having count(1)>1)select * from #t
    /*
    1           1           3
    2           1           4
    3           1           5
    4           2           NULL
    5           3           NULL(5 行受影响)
    */
      

  6.   

    if object_id('tb') is not null
       drop table tb
    go
    create table tb(a int,b int,c int)
    go
    insert into tb(a,b)
    select 1,1 union all
    select 2,1 union all
    select 3,1 union all
    select 4,2 union all
    select 5,3
    goselect * from tbupdate tb set c=a+2 from tb t1  where (select count(*) from tb where b=t1.b)>1
      

  7.   

    UPDATE TableA
    SET    c=aa.a+2
    FROM   TableA aa INNER JOIN
           (
           SELECT b
           FROM  TableA
           GROUP BY b
           Having COUNT(b)>1
           ) bb
           ON aa.b=bb.b
      

  8.   

    declare @tb table(a int,b int,c int)
    insert @tb select 1,1,null    
    union all select 2,1,null   
    union all select 3,1,null    
    union all select 4,2,null  
    union all select 5,3,null select a,b,c=case when exists(select 1 from @tb where b = t.b and a <> t.a) then a+2 else null end from @tb t