求救啊 用一条 sql 语句如何实现

寒冰冷水 2006-11-17 01:22:37
现在 在我的库存表里面 有 5条 产品的 库存记录分别是 :
1:200
2:10
3:60
4:80
5:600

现在我要出库 数量是 300 个
我要怎么做能让 库中的 记录数 变成这个样子呢 !
1:0
2:0
3:0
4:50
5:600
...全文
403 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
eboy0010 2007-01-25
  • 打赏
  • 举报
回复
偶也遇到同样的问题,学习中!
zdjionlyd 2006-11-21
  • 打赏
  • 举报
回复
看到头都有点晕了,还是看得很模糊....
dsaimh 2006-11-21
  • 打赏
  • 举报
回复
sss
zzywjing 2006-11-21
  • 打赏
  • 举报
回复
哦,先进先出法计算库存
cnszyuxin 2006-11-17
  • 打赏
  • 举报
回复
Drop Table ProductQuantity
Go
Create Table ProductQuantity
(
RowNo int,
ProductID int,
ProductQty Int
)
Insert Into ProductQuantity Values (1,23,200)
Insert Into ProductQuantity Values (2,23,10)
Insert Into ProductQuantity Values (3,23,60)
Insert Into ProductQuantity Values (4,23,80)
Insert Into ProductQuantity Values (5,23,600)
Go
Select * From ProductQuantity
Go
-- 你可以试试这一句,应该很有帮助的.
-- 从这里开始
With abcCTE (RowNo,NewNo,ProductID,ProductQty,ReduceQty,AutoID,QtyFlag) As
(
Select 0 ,0 As NewNo ,0 As ProductID,0 As ProductQty ,
300 As ReduceQty ,1 As AutoID,1
From ProductQuantity
Where RowNo=1
Union All
Select B.RowNo,B.RowNo-1,B.ProductID,B.ProductQty,
A.ReduceQty-B.ProductQty,AutoID+1,
Case When A.ReduceQty-B.ProductQty>0 Then 0
Else -1
End
From abcCTE As A Inner Join ProductQuantity As B
On B.RowNo=A.AutoID
Where A.QtyFlag>=0
)
Update ProductQuantity
Set ProductQty = Case When B.ReduceQty>0 Then 0
Else -B.ReduceQty
End
From ProductQuantity As A Inner Join abcCTE As B On A.RowNo=B.RowNo
Where QtyFlag<=0
-- 到这里结束
Go
Select * From ProductQuantity
--===========================================================
如果我的每一条记录都能够完成减少的数量那么我的所有的记录都不会变化了
有点不太明白,如果每一条都能够完成的话,那么只有一条记录变化呀.
注意在用之前一定要检测,总数量是否够减.没有细想,你要考虑哦.
cnszyuxin 2006-11-17
  • 打赏
  • 举报
回复
With abcCTE As
(
寒冰冷水 2006-11-17
  • 打赏
  • 举报
回复
上面的方法我 测试了 很好用 可是 如果 我的 每一条记录 都 能够完成 减少的数量 那么 我的所有的 记录都不会变化了 !!!!
子陌红尘 2006-11-17
  • 打赏
  • 举报
回复
declare @t table(id int,num int)
insert into @t select 1,200
insert into @t select 2,10
insert into @t select 3,60
insert into @t select 4,80
insert into @t select 5,600


declare @sum int
set @sum=300

update a
set
num=(case when (select sum(num) from @t where id<=a.id)< @sum
then 0
when
(select sum(num) from @t where id< a.id)< @sum
and
(select sum(num) from @t where id<=a.id)>=@sum
then
(select sum(num) from @t where id<=a.id)-@sum
else a.num end)
from
@t a

select * from @t
/*
id num
----------- -----------
1 0
2 0
3 0
4 50
5 600
*/

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧