安排生产计划
2009-06-13 11:01
225 查看
数据库是SQLServer,有一个基本表1如下
描述:英文字段名(中文描述)
PNO(生产单号) PQty(待产量) DayQty(机器日产量) 生产次序
001 100 40 1
002 30 20 2
003 20 10 3
想生成如下的推断计划表2:
Day(第几天) PNO(生产单号) AQty(安排产量)
1 001 40
1 002 0
1 003 0
2 001 40
2 002 0
2 003 0
3 001 20
3 002 10
3 003 0
4 002 20
4 003 0
5 003 10
6 003 10
看懂了吗?我解释一下:凡是到某天还没生产完成的单(包括生产了一部分的或者还没开始生产的),那天的数据都要包括这些单号,生产完成的单则不必显示,如果该单当天按推断不会生产,则AQty(安排产量)为0,请大家看看第3天的数据,该天001单剩下20个没做,机器对001单的日产量是40个,那么机器能够生产完001单之后还剩下半天时间,按照生产次序,001单完成之后是002单,002单要生产30个,机器对002单的日产量是20个,即是说,第3天还有半天时间可以安排生产002单,这半天时间能够生产002单是10个,002单还剩20个,这20个刚好第4天可以生产完,那么第5天就可以开始生产003单
create table t_sc(
pno varchar(10),
pqty int,
dayqty int,
orders int
)
insert into t_sc values('004',55,2,0)
insert into t_sc values('001',100,40,1)
insert into t_sc values('002',30,20,2)
insert into t_sc values('003',20,10,3)
--开始
declare @day as decimal(18,2) --所需天数
declare @day1 as decimal(18,2) --
declare @min as int --最小序号
declare @s as decimal(18,2) --剩余天数
declare @i as int --天数
set @s = 0
set @i = 1
create table #tempresult(
[day] int,
pno varchar(20),
dayqty int
)
select *,pqty/cast(dayqty as decimal(18,2)) days into #temp from t_sc --计算每个单所需工时
while ((select count(*) from #temp)>0)
begin
select @min=min(orders) from #temp --取优先序
select @day=days from #temp where orders=@min --取出去除上轮所剩天数后的天数
if (@s>0)
begin
while (@s>@day)
begin
insert into #tempresult select @i as [day],pno,dayqty*@day from #temp
where orders=@min
set @s=@s-@day
delete #temp where orders=@min
select @min=min(orders) from #temp --取优先序
select @day=days from #temp where orders=@min --取出去除上轮所剩天数后的天数
end
if (@s>0)
begin
insert into #tempresult select @i as [day],pno,dayqty*@s from #temp
where orders=@min
union all
select @i as [day],pno,0 from #temp where orders<>@min
set @day=@day-@s
set @i = @i + 1
set @s = 0 --清空剩余天数
end
end
while (@day>=1) --按整天安排
begin
insert into #tempresult select @i as [day],pno,dayqty from #temp
where orders=@min
union all
select @i as [day],pno,0 from #temp where orders<>@min
set @day = @day - 1
set @i = @i + 1
end
if (@day>0) --不足一天处理
begin
set @s=1-@day
insert into #tempresult select @i as [day],pno,dayqty*@day from #temp
where orders=@min
if(@s=0)
begin
set @i = @i + 1
end
end
delete #temp where orders=@min
end
select * From #tempresult
drop table #temp
drop table #tempresult
描述:英文字段名(中文描述)
PNO(生产单号) PQty(待产量) DayQty(机器日产量) 生产次序
001 100 40 1
002 30 20 2
003 20 10 3
想生成如下的推断计划表2:
Day(第几天) PNO(生产单号) AQty(安排产量)
1 001 40
1 002 0
1 003 0
2 001 40
2 002 0
2 003 0
3 001 20
3 002 10
3 003 0
4 002 20
4 003 0
5 003 10
6 003 10
看懂了吗?我解释一下:凡是到某天还没生产完成的单(包括生产了一部分的或者还没开始生产的),那天的数据都要包括这些单号,生产完成的单则不必显示,如果该单当天按推断不会生产,则AQty(安排产量)为0,请大家看看第3天的数据,该天001单剩下20个没做,机器对001单的日产量是40个,那么机器能够生产完001单之后还剩下半天时间,按照生产次序,001单完成之后是002单,002单要生产30个,机器对002单的日产量是20个,即是说,第3天还有半天时间可以安排生产002单,这半天时间能够生产002单是10个,002单还剩20个,这20个刚好第4天可以生产完,那么第5天就可以开始生产003单
create table t_sc(
pno varchar(10),
pqty int,
dayqty int,
orders int
)
insert into t_sc values('004',55,2,0)
insert into t_sc values('001',100,40,1)
insert into t_sc values('002',30,20,2)
insert into t_sc values('003',20,10,3)
--开始
declare @day as decimal(18,2) --所需天数
declare @day1 as decimal(18,2) --
declare @min as int --最小序号
declare @s as decimal(18,2) --剩余天数
declare @i as int --天数
set @s = 0
set @i = 1
create table #tempresult(
[day] int,
pno varchar(20),
dayqty int
)
select *,pqty/cast(dayqty as decimal(18,2)) days into #temp from t_sc --计算每个单所需工时
while ((select count(*) from #temp)>0)
begin
select @min=min(orders) from #temp --取优先序
select @day=days from #temp where orders=@min --取出去除上轮所剩天数后的天数
if (@s>0)
begin
while (@s>@day)
begin
insert into #tempresult select @i as [day],pno,dayqty*@day from #temp
where orders=@min
set @s=@s-@day
delete #temp where orders=@min
select @min=min(orders) from #temp --取优先序
select @day=days from #temp where orders=@min --取出去除上轮所剩天数后的天数
end
if (@s>0)
begin
insert into #tempresult select @i as [day],pno,dayqty*@s from #temp
where orders=@min
union all
select @i as [day],pno,0 from #temp where orders<>@min
set @day=@day-@s
set @i = @i + 1
set @s = 0 --清空剩余天数
end
end
while (@day>=1) --按整天安排
begin
insert into #tempresult select @i as [day],pno,dayqty from #temp
where orders=@min
union all
select @i as [day],pno,0 from #temp where orders<>@min
set @day = @day - 1
set @i = @i + 1
end
if (@day>0) --不足一天处理
begin
set @s=1-@day
insert into #tempresult select @i as [day],pno,dayqty*@day from #temp
where orders=@min
if(@s=0)
begin
set @i = @i + 1
end
end
delete #temp where orders=@min
end
select * From #tempresult
drop table #temp
drop table #tempresult
相关文章推荐
- Beta版本冲刺计划及安排
- 用JavaScript实现计划安排
- Beta版本冲刺计划及安排
- ERP系统模块完全解析──主生产计划MPS
- 100小时学会sap傻瓜式入门版生产计划pp篇
- beta版本冲刺计划及安排
- 5月份工作学习计划安排
- 工作计划及进度安排
- SAP PP 中关于计划订单和生产订单的日期计算
- [讨论]该怎样才能学好 生产计划与控制 ?
- 团队作业八-Beta版本冲刺计划及安排
- Beta版本冲刺前期计划及安排
- 生产计划执行系统或任务系统构建
- 工作计划与安排
- android学习目标计划和安排 2012-5-4 to 2012-5-15
- Beta版本冲刺计划及安排
- 学习计划安排 2016年底
- 2017年计划安排
- 生产计划流程
- linux----软件包管理、任务计划的安排、添加硬盘、划分磁盘配额、挂载外部设备等