日期:2014-05-17 浏览次数:20570 次
create table aaa1
( aa int,
bb int,
cc int,
dd int)
insert into aaa1 values(1,1,1,10)
insert into aaa1 values(1,2,2,40)
insert into aaa1 values(1,3,13,50)
insert into aaa1 values(1,5,15,30)
insert into aaa1 values(2,3,2,20)
insert into aaa1 values(2,5,5,60)
-- 更新
select aa,bb,cc,dd,
(select count(1) from aaa1 b
where b.aa=a.aa and b.bb<=a.bb) 'rn'
into #t
from aaa1 a
update x
set x.dd=case when y.rn is null then x.dd
when (x.cc-y.cc)<5 then y.dd
else x.dd end
from #t x
left join #t y on x.aa=y.aa and x.rn=y.rn+1
update a
set a.dd=b.dd
from aaa1 a
inner join #t b on a.aa=b.aa and a.bb=b.bb and a.cc=b.cc
select * from aaa1
/*
aa bb cc dd
----------- ----------- ----------- -----------
1 1 1 10
1 2 2 10
1&n