/*
* Disclaimer!!!
* This is not a real query being executed, but a simplified version for general vision.
* Executing it with any other tool may produce a different result.
*/
with [SubQuery1] as
(select
[PartMtl].[PartNum] as [PartMtl_PartNum],
[PartMtl].[RevisionNum] as [PartMtl_RevisionNum],
[PartMtl].[MtlSeq] as [PartMtl_MtlSeq],
[PartMtl].[MtlPartNum] as [PartMtl_MtlPartNum],
[PartMtl].[QtyPer] as [PartMtl_QtyPer],
[PartMtl].[UOMCode] as [PartMtl_UOMCode],
[PartMtl].[RelatedOperation] as [PartMtl_RelatedOperation],
[PartMtl].[PullAsAsm] as [PartMtl_PullAsAsm],
[PartMtl].[ViewAsAsm] as [PartMtl_ViewAsAsm],
[PartMtl].[PlanAsAsm] as [PartMtl_PlanAsAsm],
(0) as [Calculated_Hierarchy],
(case
when PartMtl.MtlSeq < 10 then cast(concat('000', PartMtl.MtlSeq) as nvarchar(25))
when PartMtl.MtlSeq < 100 then cast(concat('00', PartMtl.MtlSeq) as nvarchar(25))
when PartMtl.MtlSeq < 1000 then cast(concat('0', PartMtl.MtlSeq) as nvarchar(25))
else cast(PartMtl.MtlSeq as nvarchar(25))
end) as [Calculated_Ind1],
[PartMtl].[Company] as [PartMtl_Company],
[PartRev2].[Plant] as [PartRev2_Plant],
(cast(1 as decimal)) as [Calculated_ParentQty],
[PartRev2].[AltMethod] as [PartRev2_AltMethod]
from Erp.PartMtl as PartMtl
inner join Erp.PartRev as PartRev2 on
PartMtl.Company = PartRev2.Company
and PartMtl.PartNum = PartRev2.PartNum
and PartMtl.RevisionNum = PartRev2.RevisionNum
and PartMtl.AltMethod = PartRev2.AltMethod
and ( PartRev2.AltMethod = '' )
inner join (select
[PartRev3].[Company] as [PartRev3_Company],
[PartRev3].[PartNum] as [PartRev3_PartNum],
(max(PartRev3.RevisionNum)) as [Calculated_MaxRev]
from Erp.PartRev as PartRev3
where (PartRev3.Approved = true)
group by [PartRev3].[Company],
[PartRev3].[PartNum]) as GetMaxRev on
PartRev2.Company = GetMaxRev.PartRev3_Company
and PartRev2.PartNum = GetMaxRev.PartRev3_PartNum
and PartRev2.RevisionNum = GetMaxRev.Calculated_MaxRev
where (PartMtl.MtlPartNum = @PartNum)
union all
select
[PartMtl1].[PartNum] as [PartMtl1_PartNum],
[PartMtl1].[RevisionNum] as [PartMtl1_RevisionNum],
[PartMtl1].[MtlSeq] as [PartMtl1_MtlSeq],
[PartMtl1].[MtlPartNum] as [PartMtl1_MtlPartNum],
[PartMtl1].[QtyPer] as [PartMtl1_QtyPer],
[PartMtl1].[UOMCode] as [PartMtl1_UOMCode],
[PartMtl1].[RelatedOperation] as [PartMtl1_RelatedOperation],
[PartMtl1].[PullAsAsm] as [PartMtl1_PullAsAsm],
[PartMtl1].[ViewAsAsm] as [PartMtl1_ViewAsAsm],
[PartMtl1].[PlanAsAsm] as [PartMtl1_PlanAsAsm],
(Calculated_Hierarchy + 1) as [Calculated_Hierarchy2],
(case
when PartMtl1.MtlSeq < 10 then cast(concat(Calculated_Ind1, '-', '000', PartMtl1.MtlSeq) as nvarchar(25))
when PartMtl1.MtlSeq < 100 then cast(concat(Calculated_Ind1, '-', '00', PartMtl1.MtlSeq) as nvarchar(25))
when PartMtl1.MtlSeq < 1000 then cast(concat(Calculated_Ind1, '-', '0', PartMtl1.MtlSeq) as nvarchar(25))
else cast(PartMtl1.MtlSeq as nvarchar(25))
end) as [Calculated_Ind2],
[PartMtl1].[Company] as [PartMtl1_Company],
[PartRev].[Plant] as [PartRev_Plant],
(cast(PartMtl_QtyPer as decimal)) as [Calculated_ParentQty],
[SubQuery1].[PartRev2_AltMethod] as [PartRev2_AltMethod]
from SubQuery1 as SubQuery1
inner join Erp.PartMtl as PartMtl1 on
PartMtl1.Company = SubQuery1.PartMtl_Company
and PartMtl1.MtlPartNum = SubQuery1.PartMtl_PartNum
and PartMtl1.AltMethod = SubQuery1.PartRev2_AltMethod
inner join Erp.PartRev as PartRev on
PartMtl1.Company = PartRev.Company
and PartMtl1.PartNum = PartRev.PartNum
and PartMtl1.RevisionNum = PartRev.RevisionNum
and PartMtl1.AltMethod = PartRev.AltMethod
and ( PartRev.AltMethod = '' and PartRev.Approved = true ))
select
[SubQuery11].[PartMtl_PartNum] as [PartMtl_PartNum],
[SubQuery11].[PartMtl_MtlPartNum] as [PartMtl_MtlPartNum],
(SubQuery11.PartMtl_QtyPer * SubQuery11.Calculated_ParentQty) as [Calculated_QtyPer],
[Part].[PartDescription] as [Part_PartDescription],
[SubQuery11].[PartMtl_RevisionNum] as [PartMtl_RevisionNum],
[Part].[ProdCode] as [Part_ProdCode],
[Part].[RunOut] as [Part_RunOut],
(isnull(Usage.Calculated_Qty, 0)) as [Calculated_UsageLY],
(isnull(Forecast1.Calculated_Qty, 0)) as [Calculated_ForecastNY],
(case
when PartPlant.PersonID <> ''
then PartPlant.PersonID
else ProdGrup.PersonID
end) as [Calculated_ProdLine]
from SubQuery1 as SubQuery11
inner join Erp.Part as Part on
SubQuery11.PartMtl_Company = Part.Company
and SubQuery11.PartMtl_PartNum = Part.PartNum
and ( Part.InActive = 0 )
left outer join (select
[PartTran].[Company] as [PartTran_Company],
[PartTran].[PartNum] as [PartTran_PartNum],
(sum(PartTran.TranQty)) as [Calculated_Qty]
from Erp.PartTran as PartTran
where (PartTran.TranDate >= dateadd (year, -1, Constants.Today) and PartTran.TranType in ('MFG-CUS', 'STK-CUS'))
group by [PartTran].[Company],
[PartTran].[PartNum]) as Usage on
Part.Company = Usage.PartTran_Company
and Part.PartNum = Usage.PartTran_PartNum
left outer join (select
[Forecast].[Company] as [Forecast_Company],
[Forecast].[PartNum] as [Forecast_PartNum],
(sum(Forecast.ForeQty)) as [Calculated_Qty]
from Erp.Forecast as Forecast
where (Forecast.ForeDate >= @Today and Forecast.ForeDate <= dateadd (year, 1, Constants.Today))
group by [Forecast].[Company],
[Forecast].[PartNum]) as Forecast1 on
Part.Company = Forecast1.Forecast_Company
and Part.PartNum = Forecast1.Forecast_PartNum
left outer join Erp.ProdGrup as ProdGrup on
Part.Company = ProdGrup.Company
and Part.ProdCode = ProdGrup.ProdCode
inner join Erp.PartPlant as PartPlant on
SubQuery11.PartMtl_PartNum = PartPlant.PartNum
and SubQuery11.PartRev2_Plant = PartPlant.Plant
and SubQuery11.PartMtl_Company = PartPlant.Company
order by SubQuery11.PartMtl_PartNum, SubQuery11.PartMtl_RevisionNum