根据产品损耗率计算产品实际数量

来源:互联网 发布:xboxone手柄 mac 编辑:程序博客网 时间:2024/04/29 04:01

原帖地址:

http://community.csdn.net/Expert/topic/3236/3236660.xml?temp=.9309046

表T1(记录的是产品加工步骤的损耗情况)
Cp_No(产品编码)   Cp_Step(加工步骤)         Cp_Shl(损耗率)
001                    1                       0.1
001                    2                       0.15
001                    3                       0.2
002                    1                       0.3
002                    2                       0.15
003                   ...                      ...

表T2(记录产品经过加工步骤的最终数量)
Cp_No(产品编码)       finally_Sl(最终数量)
 001                      5
 002                      7
 ...                     ...
要求:
根据T2表提供的最终数量以及T1表提供的损耗率,算出每个加工步骤的实际数量得到表T3
Cp_No(产品编码)      Cp_Step(加工步骤)    Real_Sl(实际数量)
001                    1                  7.35/(1-0.1) =8.17
001                    2                  6.25/(1-0.15)=7.35
001                    3                  5/(1-0.2)    =6.25
002                    1                  8.24/(1-0.3) =11.77
002                    2                  7/(1-0.15)   =8.24

Cp_No是Varchar,Cp_Step是int,Cp_Shl是Numeric(18,4),Finally_Sl,Real_Sl是Numeric(18,4)
T2中的Finally_Sl 是经过T1中的所有加工步骤最终要得到的数量,比如001产品经过1,2,3三个步骤的最终数量是5。T3中的Real_Sl是由Finally_Sl根据每个加工步骤的损耗率得到,比如001由最终数量5可以得到步骤3的实际数量:5/(1-0.2)=6.25,然后根据6.25得到步骤2的实际数量6.25/(1-0.15)=7.35 

-----------------------------------------------------------------------------------------


--测试

--测试数据
create table T1(Cp_No varchar(10),Cp_Step int,Cp_Shl numeric(18,4))
insert T1 select '001',1,0.1
union all select '001',2,0.15
union all select '001',3,0.2
union all select '002',1,0.3
union all select '002',2,0.15

create table T2(Cp_No varchar(10),finally_Sl int)
insert T2 select '001',5
union all select '002',7
go

--方法1,直接计算(用辅助表)
select a.Cp_No,a.Cp_Step
 ,Cp_Shl=1-a.Cp_Shl,b.finally_Sl
 ,Real_Sl=cast(null as numeric(18,2))
into T3
from T1 a,T2 b
where a.Cp_No=b.Cp_No
order by a.Cp_No,a.Cp_Step desc

--计算 Real_Sl 列
declare @id varchar(10),@sl numeric(18,4)
update T3 set @sl=case @id when Cp_no then @sl else finally_Sl end/Cp_Shl
 ,Real_Sl=@sl,@id=Cp_no

--显示处理结果
select Cp_No,Cp_Step,Real_Sl
from T3
order by Cp_No,Cp_Step
go

/*--测试结果

Cp_No      Cp_Step     Real_Sl  
---------- ----------- ----------
001        1           8.17
001        2           7.35
001        3           6.25
002        1           11.76
002        2           8.24

(所影响的行数为 5 行)
--*/


--方法2,写自定义计算函数,实现直接出结果
--计算 Cp_Shl 的函数
create function f_calc(
@Cp_No varchar(10),
@Cp_Step int,
@finally_Sl int
)returns numeric(18,2)
as
begin
 declare @r numeric(18,2)
 set @r=@finally_Sl
 select @r=@r/(1-Cp_Shl) from T1
 where Cp_No=@Cp_No
  and Cp_Step>=@Cp_Step
 order by Cp_Step desc 
 return(@r)
end
go

--调用函数实现查询
select a.Cp_No,a.Cp_Step
 ,Real_Sl=dbo.f_calc(a.Cp_No,a.Cp_Step,b.finally_Sl)
from T1 a,T2 b
where a.Cp_No=b.Cp_No
go

/*--测试结果

Cp_No      Cp_Step     Real_Sl  
---------- ----------- ----------
001        1           8.17
001        2           7.35
001        3           6.25
002        1           11.76
002        2           8.24

(所影响的行数为 5 行)
--*/

--删除测试
drop table T1,T2,T3
drop function f_calc


 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=59618


原创粉丝点击