用Sql实现,要效率高的。请大家帮忙

rockswj 2004-08-03 10:05:53
表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
...全文
167 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSDMN 2004-08-03
  • 打赏
  • 举报
回复
呵呵,晚了好几年了
CSDMN 2004-08-03
  • 打赏
  • 举报
回复
--建立环境
create table T1 (
Cp_No varchar(10),
Cp_Step int,
Cp_Shl numeric(18,4)
)
go
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
go

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

--建立存储过程
create proc proc_query
as
create table #R (
Cp_No varchar(10),
Cp_Step int,
Real_Sl numeric(18,4)
)

select * into #t from t1

insert #R
select t.Cp_No,t.Cp_Step,
Real_Sl=x.finally_Sl/(1-t.Cp_Shl)
from #t t,T2 x
where t.Cp_No=x.Cp_No
and t.Cp_Step=(select max(Cp_Step) from #t where Cp_No=t.Cp_No)

delete t
from #t t
where t.Cp_Step=(select max(Cp_Step) from #t where Cp_No=t.Cp_No)

while exists (select 1 from #t)
begin
insert #R
select t.Cp_No,t.Cp_Step,
Real_Sl=x.Real_Sl/(1-t.Cp_Shl)
from #t t,#R x
where t.Cp_No=x.Cp_No
and t.Cp_Step=(select max(Cp_Step) from #t where Cp_No=t.Cp_No)
and t.Cp_Step=x.Cp_Step-1

delete t
from #t t
where t.Cp_Step=(select max(Cp_Step) from #t where Cp_No=t.Cp_No)
end

drop table #t
select * from #r

drop table #r

go

--调用
exec proc_query
--结果
/*
Cp_No Cp_Step Real_Sl
---------- ----------- --------------------
001 3 6.2500
002 2 8.2353
001 2 7.3529
002 1 11.7647
001 1 8.1699

(所影响的行数为 5 行)

*/
yb2080 2004-08-03
  • 打赏
  • 举报
回复
UP
zheninchangjiang 2004-08-03
  • 打赏
  • 举报
回复
UP
zjcxc 2004-08-03
  • 打赏
  • 举报
回复
--显示处理结果(应该加上字段限制)

select Cp_No,Cp_Step,Real_Sl
from T3
order by Cp_No,Cp_Step
zjcxc 2004-08-03
  • 打赏
  • 举报
回复
--测试

--测试数据
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

--生成T3表的处理
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 * from T3 order by Cp_No,Cp_Step
go

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

/*--测试结果

Cp_No Cp_Step Cp_Shl finally_Sl Real_Sl
---------- ----------- --------------------- ----------- --------
001 1 .9000 5 8.17
001 2 .8500 5 7.35
001 3 .8000 5 6.25
002 1 .7000 7 11.76
002 2 .8500 7 8.24

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

27,579

社区成员

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

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