select tem3.班组号,tem3.原料id,cast(原料数*总生产面积比例 as numeric(10,2)) 原料的数量 from 原料表 tem3 left join (select 班组号,sum(生产面积)/(select sum(生产面积)+0.0 from 生产表 where 班组号=tem.班组号) 总生产面积比例 from 生产表 as tem where 合同号='x' group by 班组号) tem2 on tem3.班组号=tem2.班组号
调试欢乐多
insert #原料表 select 1,1,5
insert #原料表 select 1,2,3
insert #原料表 select 2,1,2
insert #原料表 select 2,2,4create table #生产表(班组号 int,卡号 int,合同号 varchar(10),生产面积 int)
insert #生产表 values(1, 1 , 'x' , 100)
insert #生产表 values(1 , 2 , 'x' , 50 )
insert #生产表 values(1 , 3 , 'y' , 150)
insert #生产表 values(2 , 4 , 'x' , 70 )
select tem3.原料id,cast(sum(原料数*总生产面积比例) as numeric(10,2)) 原料的数量 from #原料表 tem3 left join (select 班组号,sum(生产面积)/(select sum(生产面积)+0.0 from #生产表 where 班组号=tem.班组号) 总生产面积比例 from #生产表 as tem where 合同号='x' group by 班组号) tem2 on tem3.班组号=tem2.班组号 group by tem3.原料id
go
drop table #原料表,#生产表
----题目不清,如果我写错了,楼主应该把题目说清楚,到这里来不是让别人猜题,而是让别人帮你
from
(select 班组号,sum(生产面积) as 面积 from 生产表
group by 班组号 where 合同号='x1') as t1
inner join
(select 班组号,sum(生产面积) as 面积 from 生产表
group by 班组号) as t2 on t1.班组号=t2.班组号
inner join 原料表 as t3 on t3.班组号=t1.班组号
group by t3.原料id假设:原料表中(班组号,原料id)是主键。