这是基础表中物料之一的数据:
Material yyyyww item_type TPT
E123 201919 Assembly 7
E123 201919 Assembly 6
E123 201919 Assembly 7
E123 201919 Assembly 7
E123 201919 FG 2
E123 201919 FG 2
E123 201919 FG 1
E123 201919 TEST 7
E123 201919 TEST 4
这里,item_type和TPT是计算finalTPT所需的关键列,如下所示:
MAX(TPT) AssemblyTPT 7
MAX(TPT) TestTPT 7
MAX(TPT) FG 2
Final TPT CEILING((SUM(7+7+2)/7)+1,1) 4 <--final result.
我正在尝试使用案例陈述进行计算,但是可以使用多种材料。因此,我需要为每种基材找到“最终TPT”。
预期输出:4
答案 0 :(得分:0)
create table #tempT (Material varchar(10),yyyyww int,item_type varchar(10),TPT int )
-------------Insert---------------
insert into #tempT values('E123' , 201919 , 'Assembly' , 7)
insert into #tempT values('E123' , 201919 , 'Assembly' , 6)
insert into #tempT values('E123' , 201919 , 'Assembly' , 7)
insert into #tempT values('E123' , 201919 , 'Assembly' , 7)
insert into #tempT values('E123' , 201919 , 'FG' , 2)
insert into #tempT values('E123' , 201919 , 'FG' , 2)
insert into #tempT values('E123' , 201919 , 'FG' , 1)
insert into #tempT values('E123' , 201919 , 'TEST' , 7)
insert into #tempT values('E123' , 201919 , 'TEST' , 4)
------------Output-------------
select Material ,ceiling(((sum((MaxTPT)))/7.00)+1)MaxTPT
from(select Material,item_type,case when item_type='Assembly' then max(TPT)
when item_type='fg' then max(TPT)
when item_type='TEST' then max(TPT) end MaxTPT from #tempT
group by Material,item_type)a
group by Material