27,579
社区成员
发帖
与我相关
我的任务
分享
create table lin
(id int,
存货名称 varchar(5),
存货数量 int,
需求数量 int,
订购数量 int)
insert into lin
select 1, 'AA', 1300, 800, 0 union all
select 2, 'AA', 1300, 600, 100 union all
select 3, 'AA', 1300, 300, 300 union all
select 4, 'AA', 1300, 200, 200
update a
set a.存货数量=
case when (a.存货数量-(select isnull(sum(需求数量),0) from lin b where b.id<a.id))<0
then 0 else a.存货数量-(select isnull(sum(需求数量),0) from lin b where b.id<a.id) end
from lin a
select * from lin
/*
id 存货名称 存货数量 需求数量 订购数量
----------- ----- ----------- ----------- -----------
1 AA 1300 800 0
2 AA 500 600 100
3 AA 0 300 300
4 AA 0 200 200
(4 row(s) affected)
*/