declare @t table(noid int,fType int,fQty int,fAmount int)
insert @t
select 1, 1, 30, 300 union all
select 2, 2, 20, 200 union all
select 3, 2, 30, 310 union all
select 4, 3, 25, NULL union all
select 5, 3, 20, NULL union all
select 6, 2, 10, 130 union all
select 7, 3, 9, NULL
----更新
UPDATE a SET fAmount =
fqty*((select
sum(case
when fType = 1 or fType = 2 then isnull(famount,0)
when fType = 3 then -isnull(famount,0) else 0
end)/
sum(case
when fType = 1 or fType = 2 then isnull(fqty,0)
when fType = 3 then -isnull(fqty,0) else 0
end)
from @t where noid <= a.noid))
FROM @t as a WHERE fType = 3
----查看
select * from @t
declare @t table(noid int,fType int,fQty int,fAmount int)
insert @t
select 1, 1, 30, 300 union all
select 2, 2, 20, 200 union all
select 3, 2, 30, 310 union all
select 4, 3, 25, NULL union all
select 5, 3, 20, NULL union all
select 6, 2, 10, 130 union all
select 7, 3, 9, NULL
----更新
UPDATE a SET fAmount =
(select sum(case when fType = 1 or fType = 2 then fQty when fType = 3 then -fQty else 0 end)
from @t where noid <= a.noid)
FROM @t as a WHERE fType = 3
----查看
select * from @t