select
[ProdGrup].[Description] as [ProdGrup_Description],
[JobHead].[JobNum] as [JobHead_JobNum],
[JobHead].[PartNum] as [JobHead_PartNum],
[JobHead].[ProdQty] as [JobHead_ProdQty],
(sum( PartTran.TranQty )) as [Calculated_ReceivedToStock],
[JobAsmbl].[PartNum] as [JobAsmbl_PartNum],
[JobAsmbl].[AssemblySeq] as [JobAsmbl_AssemblySeq],
[JobAsmbl].[RequiredQty] as [JobAsmbl_RequiredQty],
[JobMtlsub].[Calculated_TotalIssued] as [Calculated_TotalIssued],
[JobMtlsub].[JobMtl_QtyPer] as [JobMtl_QtyPer],
(ReceivedToStock*JobMtlsub.JobMtl_QtyPer) as [Calculated_TotalComplete],
(JobMtlsub.Calculated_TotalIssued-TotalComplete) as [Calculated_Diff],
(Diff/JobMtlsub.Calculated_TotalIssued*100) as [Calculated_ScrapPerc],
[JobHead].[ClosedDate] as [JobHead_ClosedDate]
from Erp.JobHead as JobHead
inner join Erp.PartTran as PartTran on
JobHead.Company = PartTran.Company
and JobHead.JobNum = PartTran.JobNum
and ( PartTran.TranType in ('MFG-CUS', 'MFG-STK') )
inner join Erp.JobAsmbl as JobAsmbl on
JobHead.Company = JobAsmbl.Company
and JobHead.JobNum = JobAsmbl.JobNum
inner join (select
[JobMtl].[JobNum] as [JobMtl_JobNum],
[JobMtl].[AssemblySeq] as [JobMtl_AssemblySeq],
[JobMtl].[MtlSeq] as [JobMtl_MtlSeq],
[JobMtl].[QtyPer] as [JobMtl_QtyPer],
[JobMtl].[RequiredQty] as [JobMtl_RequiredQty],
(sum( JobMtl.IssuedQty )) as [Calculated_TotalIssued]
from Erp.JobMtl as JobMtl
group by [JobMtl].[JobNum],
[JobMtl].[AssemblySeq],
[JobMtl].[MtlSeq],
[JobMtl].[QtyPer],
[JobMtl].[RequiredQty]) as JobMtlsub on
JobAsmbl.JobNum = JobMtlsub.JobMtl_JobNum
and JobAsmbl.AssemblySeq = JobMtlsub.JobMtl_AssemblySeq
inner join Erp.ProdGrup as ProdGrup on
JobHead.Company = ProdGrup.Company
and JobHead.ProdCode = ProdGrup.ProdCode
and ( ProdGrup.ProdCode = @Group )
where (JobHead.JobNum like '18%' and JobHead.ClosedDate >= @StartDate and JobHead.ClosedDate <= @EndDate)
group by [ProdGrup].[Description],
[JobHead].[JobNum],
[JobHead].[PartNum],
[JobHead].[ProdQty],
[JobAsmbl].[PartNum],
[JobAsmbl].[AssemblySeq],
[JobAsmbl].[RequiredQty],
[JobMtlsub].[Calculated_TotalIssued],
[JobMtlsub].[JobMtl_QtyPer],
[JobHead].[ClosedDate]