Sure thing! Don’t make too much fun of my poor BAQ building
/*
* 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 [UnionGrab] as
(select
(Case
when SOByMonth1.OrderRel_Company is not null then SOByMonth1.OrderRel_Company
when ForeQty.Forecast_Company is not null then ForeQty.Forecast_Company
when MPSQty.MasProd_Company is not null then MPSQty.MasProd_Company
when FirmJobQty.JobHead_Company is not null then FirmJobQty.JobHead_Company
when UnfirmJobQty.JobHead1_Company is not null then UnfirmJobQty.JobHead1_Company
when OHQty2.PartBin_Company is not null then OHQty2.PartBin_Company
end) as [Calculated_Company],
(Case
when SOByMonth1.Calculated_MonthYear is not null then SOByMonth1.Calculated_MonthYear
when ForeQty.Calculated_MonthYear is not null then ForeQty.Calculated_MonthYear
when MPSQty.Calculated_YearMonth is not null then MPSQty.Calculated_YearMonth
when FirmJobQty.Calculated_YearMonth is not null then FirmJobQty.Calculated_YearMonth
when UnfirmJobQty.Calculated_YearMonth is not null then UnfirmJobQty.Calculated_YearMonth
when OHQty2.Calculated_CurrentYearMonth is not null then OHQty2.Calculated_CurrentYearMonth
end) as [Calculated_YearMonth03],
(case
when SOByMonth1.OrderRel_PartNum is not null then SOByMonth1.OrderRel_PartNum
when ForeQty.Forecast_PartNum is not null then ForeQty.Forecast_PartNum
when MPSQty.MasProd_PartNum is not null then MPSQty.MasProd_PartNum
when FirmJobQty.JobHead_PartNum is not null then FirmJobQty.JobHead_PartNum
when UnfirmJobQty.JobHead1_PartNum is not null then UnfirmJobQty.JobHead1_PartNum
when OHQty2.PartBin_PartNum is not null then OHQty2.PartBin_PartNum
end) as [Calculated_Part],
(case
when SOByMonth1.Calculated_OpenQty is null then 0.00 else SOByMonth1.Calculated_OpenQty
end) as [Calculated_OpenSOQty],
(case
when ForeQty.Calculated_QtyOpen is null then 0.00 else ForeQty.Calculated_QtyOpen
end) as [Calculated_OpenForeQTY],
(case
when MPSQty.Calculated_MPSQty is null then 0.00 else MPSQty.Calculated_MPSQty
end) as [Calculated_OpenMPSQty],
(case
when FirmJobQty.Calculated_JobQty is null then 0.00 else FirmJobQty.Calculated_JobQty
end) as [Calculated_OpenFirmJobQty],
(case
when UnfirmJobQty.Calculated_UnfirmJobQty is null then 0.00 else UnfirmJobQty.Calculated_UnfirmJobQty
end) as [Calculated_OpenUnfirmJobQty],
(case
when OHQty2.Calculated_OHQty is null then 0.00 else OHQty2.Calculated_OHQty
end) as [Calculated_OpenOHQty],
(OpenFirmJobQty+ OpenUnfirmJobQty) as [Calculated_Production],
(OpenSOQty+ OpenForeQTY) as [Calculated_Demand],
(Production- Demand) as [Calculated_Change],
(OpenOHQty) as [Calculated_Total]
from (select
[OrderRel].[Company] as [OrderRel_Company],
[OrderRel].[PartNum] as [OrderRel_PartNum],
(sum((OrderRel.OurJobQty- OrderRel.OurJobShippedQty)+ (OrderRel.OurStockQty- OrderRel.OurStockShippedQty))) as [Calculated_OpenQty],
(convert(varchar, year(OrderRel.ReqDate), 101)+ '-'+ convert(varchar, month(OrderRel.ReqDate), 101)) as [Calculated_MonthYear],
('SOByMonth') as [Calculated_Catagory]
from Erp.OrderRel as OrderRel
inner join Erp.OrderDtl as OrderDtl on
OrderRel.Company = OrderDtl.Company
and OrderRel.OrderNum = OrderDtl.OrderNum
and OrderRel.OrderLine = OrderDtl.OrderLine
and ( OrderDtl.OpenLine = 1 )
inner join Erp.OrderHed as OrderHed on
OrderDtl.Company = OrderHed.Company
and OrderDtl.OrderNum = OrderHed.OrderNum
and ( OrderHed.OpenOrder = 1 )
where (OrderRel.OpenRelease = 1)
group by [OrderRel].[Company],
[OrderRel].[PartNum],
(convert(varchar, year(OrderRel.ReqDate), 101)+ '-'+ convert(varchar, month(OrderRel.ReqDate), 101))) as SOByMonth1
full outer join (select
[Forecast].[Company] as [Forecast_Company],
[Forecast].[PartNum] as [Forecast_PartNum],
(sum(Forecast.ForeQty- Forecast.ConsumedQty)) as [Calculated_QtyOpen],
(convert(varchar, year(Forecast.ForeDate), 101)+ '-'+ convert(varchar, month(Forecast.ForeDate), 101)) as [Calculated_MonthYear],
('ForeQty') as [Calculated_Catagory]
from Erp.Forecast as Forecast
where (Forecast.Inactive = 0 and Forecast.ForeQty > Forecast.ConsumedQty)
group by [Forecast].[Company],
[Forecast].[PartNum],
(convert(varchar, year(Forecast.ForeDate), 101)+ '-'+ convert(varchar, month(Forecast.ForeDate), 101))) as ForeQty on
ForeQty.Forecast_Company = SOByMonth1.OrderRel_Company
and ForeQty.Forecast_PartNum = SOByMonth1.OrderRel_PartNum
and ForeQty.Calculated_MonthYear = SOByMonth1.Calculated_MonthYear
full outer join (select
[MasProd].[Company] as [MasProd_Company],
[MasProd].[PartNum] as [MasProd_PartNum],
(sum(MasProd.ProdQty)) as [Calculated_MPSQty],
(convert(varchar, year(MasProd.DueDate), 101)+ '-'+ convert(varchar, month(MasProd.DueDate), 101)) as [Calculated_YearMonth],
('MPSQty') as [Calculated_Catagory]
from Erp.MasProd as MasProd
group by [MasProd].[Company],
[MasProd].[PartNum],
(convert(varchar, year(MasProd.DueDate), 101)+ '-'+ convert(varchar, month(MasProd.DueDate), 101))) as MPSQty on
MPSQty.MasProd_Company = ForeQty.Forecast_Company
and MPSQty.MasProd_PartNum = ForeQty.Forecast_PartNum
and MPSQty.Calculated_YearMonth = ForeQty.Calculated_MonthYear
full outer join (select
[JobHead].[Company] as [JobHead_Company],
[JobHead].[PartNum] as [JobHead_PartNum],
(sum(JobHead.ProdQty)) as [Calculated_JobQty],
(convert(varchar, year(JobHead.DueDate), 101)+ '-'+ convert(varchar, month(JobHead.DueDate), 101)) as [Calculated_YearMonth],
('FirmJobQty') as [Calculated_Catagory]
from Erp.JobHead as JobHead
where (JobHead.JobClosed = 0 and JobHead.JobComplete = 0 and JobHead.JobFirm = 1)
group by [JobHead].[Company],
[JobHead].[PartNum],
(convert(varchar, year(JobHead.DueDate), 101)+ '-'+ convert(varchar, month(JobHead.DueDate), 101))) as FirmJobQty on
SOByMonth1.OrderRel_Company = FirmJobQty.JobHead_Company
and SOByMonth1.OrderRel_PartNum = FirmJobQty.JobHead_PartNum
and SOByMonth1.Calculated_MonthYear = FirmJobQty.Calculated_YearMonth
full outer join (select
[JobHead1].[Company] as [JobHead1_Company],
[JobHead1].[PartNum] as [JobHead1_PartNum],
(sum(JobHead1.ProdQty)) as [Calculated_UnfirmJobQty],
(convert(varchar, year(JobHead1.DueDate), 101)+ '-'+ convert(varchar, month(JobHead1.DueDate), 101)) as [Calculated_YearMonth],
('UnfirmJobQty') as [Calculated_Catagory]
from Erp.JobHead as JobHead1
where (JobHead1.JobClosed = 0 and JobHead1.JobComplete = 0 and JobHead1.JobFirm = 0)
group by [JobHead1].[Company],
[JobHead1].[PartNum],
(convert(varchar, year(JobHead1.DueDate), 101)+ '-'+ convert(varchar, month(JobHead1.DueDate), 101))) as UnfirmJobQty on
FirmJobQty.JobHead_Company = UnfirmJobQty.JobHead1_Company
and FirmJobQty.JobHead_PartNum = UnfirmJobQty.JobHead1_PartNum
and FirmJobQty.Calculated_YearMonth = UnfirmJobQty.Calculated_YearMonth
full outer join (select
[PartBin].[Company] as [PartBin_Company],
[PartBin].[PartNum] as [PartBin_PartNum],
(sum(PartBin.OnhandQty)) as [Calculated_OHQty],
(FORMAT (Constants.Today, 'yyyy-MM')) as [Calculated_CurrentYearMonth],
('OHQty') as [Calculated_Catagory]
from Erp.PartBin as PartBin
inner join Erp.WhseBin as WhseBin on
PartBin.Company = WhseBin.Company
and PartBin.WarehouseCode = WhseBin.WarehouseCode
and PartBin.BinNum = WhseBin.BinNum
and ( WhseBin.NonNettable = 0 )
where (PartBin.OnhandQty > 0)
group by [PartBin].[Company],
[PartBin].[PartNum]) as OHQty2 on
UnfirmJobQty.JobHead1_Company = OHQty2.PartBin_Company
and UnfirmJobQty.JobHead1_PartNum = OHQty2.PartBin_PartNum
and UnfirmJobQty.Calculated_YearMonth = OHQty2.Calculated_CurrentYearMonth
union all
select
[UnionGrab1].[Calculated_Company] as [Calculated_Company],
[UnionGrab1].[Calculated_YearMonth03] as [Calculated_YearMonth03],
[UnionGrab1].[Calculated_Part] as [Calculated_Part],
[UnionGrab1].[Calculated_OpenSOQty] as [Calculated_OpenSOQty],
[UnionGrab1].[Calculated_OpenForeQTY] as [Calculated_OpenForeQTY],
[UnionGrab1].[Calculated_OpenMPSQty] as [Calculated_OpenMPSQty],
[UnionGrab1].[Calculated_OpenFirmJobQty] as [Calculated_OpenFirmJobQty],
[UnionGrab1].[Calculated_OpenUnfirmJobQty] as [Calculated_OpenUnfirmJobQty],
[UnionGrab1].[Calculated_OpenOHQty] as [Calculated_OpenOHQty],
[UnionGrab1].[Calculated_Production] as [Calculated_Production],
[UnionGrab1].[Calculated_Demand] as [Calculated_Demand],
[UnionGrab1].[Calculated_Change] as [Calculated_Change],
(sum(UnionGrab1.Calculated_Change) OVER (PARTITION BY UnionGrab1.Calculated_Part ORDER BY UnionGrab1.Calculated_YearMonth03)) as [Calculated_Total]
from UnionGrab as UnionGrab1)
select
[UnionGrab3].[Calculated_Company] as [Calculated_Company],
[UnionGrab3].[Calculated_YearMonth03] as [Calculated_YearMonth03],
[UnionGrab3].[Calculated_Part] as [Calculated_Part],
[UnionGrab3].[Calculated_OpenSOQty] as [Calculated_OpenSOQty],
[UnionGrab3].[Calculated_OpenForeQTY] as [Calculated_OpenForeQTY],
[UnionGrab3].[Calculated_OpenMPSQty] as [Calculated_OpenMPSQty],
[UnionGrab3].[Calculated_OpenFirmJobQty] as [Calculated_OpenFirmJobQty],
[UnionGrab3].[Calculated_OpenUnfirmJobQty] as [Calculated_OpenUnfirmJobQty],
[UnionGrab3].[Calculated_OpenOHQty] as [Calculated_OpenOHQty],
[UnionGrab3].[Calculated_Production] as [Calculated_Production],
[UnionGrab3].[Calculated_Demand] as [Calculated_Demand],
[UnionGrab3].[Calculated_Change] as [Calculated_Change],
[UnionGrab3].[Calculated_Total] as [Calculated_Total]
from UnionGrab as UnionGrab3