ID  UserID  Money   FFR
1   1       100     李明
2   1       200     王丽
3   1       300   
4   1       400   
5   1       500   
6   2       130     江河
7   2       180     李小三
8   2       180 
9   3       300
10  3       300数据如上所示,希望得到结果ID  UserID  Money   FFR
1   1       100     李明
2   1       200     王丽
3   1       300     王丽
4   1       400     王丽
5   1       500     王丽
6   2       130     江河
7   2       180     李小三
8   2       180     李小三
9   3       300
10  3       300就是把 money 不为空的数据行的 FFR 更新为最后一次出现 FFR 的值

解决方案 »

  1.   

    update 表 t
    set
        FFR=(select top 1 FFR from 表 where UserID=t.UserID and ID<t.ID order by ID desc)
    where
        t.money is not null and t.FFR is null
      

  2.   

    楼上的order by ID desc是不是改成order by FFR desc 更好?
      

  3.   

    ID  UserID  Money   FFR
    1   1       100     李明
    2   1       200     
    3   1       300     王丽
    4   1       400   
    5   1       500   
    6   2       130     江河
    7   2       180     李小三
    8   2       180 
    9   3       300
    10  3       300
    楼上的,不知道就少在这说~
    如果表的数据如上,那么就过就会变成ID  UserID  Money   FFR
    1   1       100     李明
    2   1       200     
    3   1       300     
    4   1       400     
    5   1       500     
    6   2       130     江河
    7   2       180     李小三
    8   2       180     李小三
    9   3       300
    10  3       300
      

  4.   

    declare @t table(ID int,  UserID int,  Money int,   FFR varchar(10))
    insert @t select 1   ,1       ,100,     '李明'
    union all select 2  , 1       ,200,     '王丽'
    union all select 3  , 1       ,300,   ''
    union all select 4 ,  1       ,400,   ''
    union all select 5 ,  1       ,500,   ''
    union all select 6 ,  2       ,130,     '江河'
    union all select 7 ,  2       ,180,     '李小三'
    union all select 8   ,2       ,180, ''
    union all select 9,   3       ,300,''
    union all select 10,  3       ,300,''update a set ffr=b.ffr from @t a inner join (select id,userid,ffr from @t x inner join (select max(id) mxid from @t where ffr<>'' group by userid) y on id=mxid where ffr<>'') b on a.userid=b.userid where a.ffr=''
    select * from @t
    /*结果
    1 1 100 李明
    2 1 200 王丽
    3 1 300 王丽
    4 1 400 王丽
    5 1 500 王丽
    6 2 130 江河
    7 2 180 李小三
    8 2 180 李小三
    9 3 300
    10 3 300
    */
      

  5.   

    如果你的数据表中不是以空串''存的而是null那么语句中将 ffr<>''或ffr=''改为 is not null或 is null即可