试着解决http://topic.youkuaiyun.com/u/20090610/20/92c3d2eb-4ee5-4df1-bc09-2fd32254fa06_3.html

优快云网友发布了一个题目,如下连接所示:

http://topic.youkuaiyun.com/u/20090610/20/92c3d2eb-4ee5-4df1-bc09-2fd32254fa06_3.html

 

个人比较感兴趣,于是试着给了如下答案:

 

首先创建表:

CREATE TABLE [dbo].[ProductOrder](
 [pno] [int] NULL,
 [pQty] [int] NULL,
 [DayQty] [int] NULL,
 [ProdOrder] [int] NULL
) ON [PRIMARY]

 

插入如下值:

1 100 40 1
2 30 20 2
3 20 10 3

创建下表保存结果:

create table DailyProd
(
[day] int,
pno int,
AQty int
)

下面是过程

 

truncate table DailyProd
select * into ProductOrder_tmp from ProductOrder --临时表存放当前未生产完的产品
declare @day int
set @day=1  --天数计时器
while(exists(select * from ProductOrder_tmp)) --知道所有的产品被生产完
begin
 declare prodCur cursor  for select * from ProductOrder_tmp order by prodOrder asc
 open prodCur
 
 declare @pno int,@pqty int,@Dayqty int,@PO int,@sum float
 fetch next from prodCur into @pno,@pQty,@DayQty,@PO
 
 set @sum=1.0 --每个
 while(@@fetch_status=0)
 begin
   if(@pQty>@DayQty*@sum)--该类产品今天没有能力生产完
   begin
     update ProductOrder_tmp set pqty=pqty-dayqty*@sum where pno=@pno
     insert into DailyProd values(@day,@pno,@DayQty*@sum)
     set @sum=0
   end 
   else --今天之后没有余量了
   begin
     delete from ProductOrder_tmp where  pno=@pno
     insert into DailyProd values(@day,@pno,@pQty)
     set @sum=@sum-(cast(@pQty as float)/@DayQty)
   end
   fetch next from prodCur into @pno,@pQty,@DayQty,@PO
 end

 --清场
 close prodCur
 deallocate prodCur
 set @day=@day+1

end

select * from DailyProd --显示结果
drop table ProductOrder_tmp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值