由于开始发贴误发到asp论坛里,所以分少了点。见谅,要分的可以到 asp论坛里去答,谢谢。
表1 如下:
年, 姓名,英语,去年英语
2008, 小张, 60, 80
2008, 小王, 50, 60
2009, 小王, 80, 50
2009, 小张, 60, 60
2010, 小王, 70, <null>
2010, 小张, 90, <null>
求一条update sql语句后表1内容如下:注意:不是select 查询,一定要将数所写进表里(虽然这么做没有意义,特殊要求)。
年, 姓名,英语,去年英语
2008, 小张, 60, 80
2008, 小王, 50, 60
2009, 小王, 80, 50
2009, 小张, 60, 60
2010, 小王, 70, 80 /*将小王去年(2009年)英语得分80写入*/
2010, 小张, 90, 60 /*将小张去年(2009年)英语得分60写入*/

解决方案 »

  1.   

    update a set 去年英语=(select top 1 英语 from [Table] where 姓名=a.姓名 and 年=a.年-1)
    from [Table] a
    where 去年英语 is null
      

  2.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([年] varchar(5),[姓名] varchar(5),[英语] int,[去年英语] int)
    go
    insert [tb]
    select '2008','小张',60,80 union all
    select '2008','小王',50,60 union all
    select '2009','小王',80,50 union all
    select '2009','小张',60,60 union all
    select '2010','小王',70,null union all
    select '2010','小张',90,nullupdate t
    set [去年英语] = (select [英语] from [tb] where [年] = t.[年] - 1 and [姓名] = t.[姓名])
    from [tb] t
    where [去年英语] is nullselect * from [tb](2 行受影响)
    年     姓名    英语          去年英语
    ----- ----- ----------- -----------
    2008  小张    60          80
    2008  小王    50          60
    2009  小王    80          50
    2009  小张    60          60
    2010  小王    70          80
    2010  小张    90          60
      

  3.   

    update [表1] set 去年英语=b.去年英语 from [表1] b where [表1].年份=b.年份+1 and [表1].姓名=b.姓名 and [表1].去年英语 is null