现在我有一张表A,有字段a int ,b  nvarchar, c smalldatetime ,d smalldatetime 
现在我要做得是把 d = c 
有个前提条件是b相等,而且a = a + 1
例如: 
a     b          c          d
--------------------------------- 
1    a          20080101             
2    a          20080102             
3    a          20080103             
4    b          20080102              
5    c          20080103              
结果应该为: 
a    b          c         d
--------------------------------- 
1    a        20080101   20080102  
2    a        20080102   20080103        
3    a        20080103   null        
4    b        20080102   null      
5    c        20080103   null      

解决方案 »

  1.   

    select a,b,c, d=(select min(c) from tb where b = t.b and b>t.b) from tb t
      

  2.   


    update tb
    set d=a.c
    from tb a
    where a.b=tb.b and a.a=tb.a+1
      

  3.   

    select a,b,c, d=(select min(c) from tb where b = t.b and b>t.b) from tb t
      

  4.   

    create table tb(a varchar(10) , b varchar(10) , c varchar(10) )
    insert into tb values('1' , 'a' , '20080101') 
    insert into tb values('2' , 'a' , '20080102') 
    insert into tb values('3' , 'a' , '20080103') 
    insert into tb values('4' , 'b' , '20080102') 
    insert into tb values('5' , 'c' , '20080103') 
    goselect * , d = (select c from tb where b = t.b and a = t.a + 1 ) from tb tdrop table tb/*
    a          b          c          d          
    ---------- ---------- ---------- ---------- 
    1          a          20080101   20080102
    2          a          20080102   20080103
    3          a          20080103   NULL
    4          b          20080102   NULL
    5          c          20080103   NULL(所影响的行数为 5 行)
    */
      

  5.   

    update a1 set
       d=a2.c
    from a a1,a a2
    where a2.a=a1.a+1
    and a2.b=a1.b
      

  6.   

    NND,我2楼写错了,你们也照着抄?
      

  7.   

    create table tb(a varchar(10) , b varchar(10) , c varchar(10) , d varchar(10))
    insert into tb values('1' , 'a' , '20080101',null) 
    insert into tb values('2' , 'a' , '20080102',null) 
    insert into tb values('3' , 'a' , '20080103',null) 
    insert into tb values('4' , 'b' , '20080102',null) 
    insert into tb values('5' , 'c' , '20080103',null) 
    go
    --查询
    select a , b , c, d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t--更新
    update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb tdrop table tb/*
    a          b          c          d          
    ---------- ---------- ---------- ---------- 
    1          a          20080101   20080102
    2          a          20080102   20080103
    3          a          20080103   NULL
    4          b          20080102   NULL
    5          c          20080103   NULL(所影响的行数为 5 行)
    */
      

  8.   

    我要做得事情是:d 的日期数值是a的值+1对应的a值,再对应的c值,
    赋给d
      

  9.   

    强啊
    update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t加了不连续a相同b后再连续a相同b,测试通过!呵呵
      

  10.   

    update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
    很强大