17,086
社区成员
发帖
与我相关
我的任务
分享
create table tb(工艺序号 int,投入量 int,转换率 int)
insert into tb select 1,20,1
insert into tb select 2, 0,2
insert into tb select 3, 0,1
insert into tb select 4,10,2
insert into tb select 5, 0,3
insert into tb select 6, 5,2
go
;with cte as(
select *,投入量*转换率 as 应产出量 from tb where 工艺序号=1
union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
)
select * from cte
/*
工艺序号 投入量 转换率 应产出量
----------- ----------- ----------- -----------
1 20 1 20
2 0 2 40
3 0 1 40
4 10 2 100
5 0 3 300
6 5 2 610
(6 行受影响)
*/
go
drop table tb
union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
--是这句错了。。。不知道怎么改。。。
create table tb(工艺序号 int,投入量 int,转换率 int)
insert into tb select 1,20,1
insert into tb select 2, 0,2
insert into tb select 3, 0,1
insert into tb select 4,10,2
insert into tb select 5, 0,3
insert into tb select 6, 5,2
go
;with cte as(
select tb.*,投入量*转换率 应产出量 from tb where 工艺序号=1
union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
)
select * from cte
go
drop table tb