I am trying to merge output from 3 selection processes. Here is the code that I am using and then I will show the error that I am receiving when trying to execute.
select COALESCE(a.[Class],b.[Class],c.[Class]) AS [Class],
COALESCE(a.[Material],b.[Material],d.[Material]) AS [Material],
COALESCE(a.[Description],b.[Description],c.[Description]) AS [Description],
COALESCE(a.[A],0) + coalesce(b.[A],0) + coalesce(c.[A],0) AS [On Hand],
COALESCE(a.[B],0) + coalesce(b.[B],0) + coalesce(c.[B],0) AS [On Order],
COALESCE(a.[C],0) + coalesce(b.[C],0) + coalesce(c.[C],0) AS [Picked],
COALESCE(a.[D],0) + coalesce(b.[D],0) + coalesce(c.[D],0) AS [Job]
from
(
Select (a.Material) As [Material]
,Sum(Case when ML.Qty_On_Hand > 0 then ML.Qty_On_Hand Else 0 end) [A]
,Sum(Case when a.Sales_Code = 'XX' then 0 Else 0 end) [B]
,Sum(Case when a.Sales_Code = 'XX' then 0 Else 0 end) [C]
,Sum(Case when a.Sales_Code = 'XX' then 0 Else 0 end) [D]
from [PRODUCTION].dbo.Material As a
left outer join [PRODUCTION].dbo.Material_Location As ML
on a.Material = ML.Material
where a.Sales_Code = '5-Day'
GROUP BY a.Class, a.Material
)a
full join
(
Select (b.Material) As [Material]
,Sum(Case when b.Sales_Code = 'XX' then 0 Else 0 end) [A]
,Sum(Case when SD.Order_Qty > 0 then (SD.Qrder_Qty - SD.Shipped_Qty) Else 0 end) [B]
,Sum(Case when SD.Picked_Qty > 0 then SD.Picked_Qty Else 0 end) [C]
,Sum(Case when b.Sales_Code = 'XX' then 0 Else 0 end) [D]
from [PRODUCTION].dbo.Material As b
left outer join [PRODUCTION].dbo.SO_Detail As SD
on b.Material = SD.Material and (SD.Status = 'Open' or SD.Status = 'Backorder')
where b.Sales_Code = '5-Day'
GROUP BY b.Class, b.Material
)b
full join
(
Select (c.Material) As [Material]
,Sum(Case when c.Sales_Code = 'XX' then 0 ELSE 0 end) [A]
,Sum(Case when c.Sales_Code = 'XX' then 0 Else 0 end) [B]
,Sum(Case when c.Sales_Code = 'XX' then 0 Else 0 end) [C]
,Sum(Case when J.In_Production_Quantity > 0 then J.In_Producion_Quantity Else 0 end) [D]
from [PRODUCTION].dbo.Material As c
left outer join [PRODUCTION].dbo.Job As J
on c.Material = J.Material and J.Status = 'Active'
where c.Sales_Code = '5-Day'
GROUP BY c.Class, c.Material
)c
on a.[Class] = b.[Class] and a.[Class] = c.[Class] and a.[Material] = b.[Material] and a.[Material] = c.[Material]
Order by COALESCE((a.[Class],b.[Class],c.[Class]),(a.[Material],b.[Material],c.[Material]))
Error
Msg 156, Level 15, State 1, Line 48Incorrect syntax near the keyword 'Order'.
SWProduction