表如下: 
 a(单位标识)   b(变更日期)  c(区划)d(情况) 
甲公司           2000-1-1      广州
 甲公司          1999-1-1      深圳
 乙公司           2000-1-1     广州 
 乙公司          2002-1-1      深圳
想写个脚本 在同一个公司的2条记录为一组 把 c=广州 b(日期)大 的那条记录的d中写入 ‘新’
           在同一个公司的2条记录为一组 把 c=广州 b(日期)小 的那条记录的d中写入 ‘旧’ 
例如 甲公司 区划为广州的这条 写入 新   乙公司 区划为广州的这条 写入 旧
谁能帮忙给个脚本啊 

解决方案 »

  1.   

    update t
    set d =( case when b = (select max(b) from table where a = t.a and c= t.c) then '新' else '旧' end)
    from table t
      

  2.   

    我上面的条件有点错了,看这个
    declare @t table(a char(10),b datetime,c char(4),d char(2))
    insert @t select '甲公司','2000-1-1','广州',' '
    insert @t select '甲公司','1999-1-1','深圳','' 
    insert @t select '乙公司','2000-1-1','广州',''   
    insert @t select '乙公司','2002-1-1','深圳',''update t 
    set   d   =(   
               case when b =(select max(b) from  @t where a <> t.a and c= t.c)then   '新 '   else   '旧 '   end) 
    from   @t   t 
    select * from @t/*a          b                                                      c    d    
    ---------- ------------------------------------------------------ ---- ---- 
    甲公司        2000-01-01 00:00:00.000                                广州   新
    甲公司        1999-01-01 00:00:00.000                                深圳   旧
    乙公司        2000-01-01 00:00:00.000                                广州   新
    乙公司        2002-01-01 00:00:00.000                                深圳   旧*/
      

  3.   

    哈哈,又错了--- declare @t table(a char(10),b datetime,c char(4),d char(2))
    insert @t select '甲公司','2000-1-1','广州',' '
    insert @t select '甲公司','1999-1-1','深圳','' 
    insert @t select '乙公司','2000-1-1','广州',''   
    insert @t select '乙公司','2002-1-1','深圳',''update t 
    set   d   =(   
               case when b =(select max(b) from  @t where  c= t.c)then   '新 '   else   '旧 '   end) 
    from   @t   t 
    select * from @t/*a          b                                                      c    d    
    ---------- ------------------------------------------------------ ---- ---- 
    甲公司        2000-01-01 00:00:00.000                                广州   新
    甲公司        1999-01-01 00:00:00.000                                深圳   旧
    乙公司        2000-01-01 00:00:00.000                                广州   新
    乙公司        2002-01-01 00:00:00.000                                深圳   新
    */