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 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 的值
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
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
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
*/