I am attaching the SQL CTE Code with error message and DDL. Please review to see if you can help solve this error.
First, I have never done anything with CTE before.Here is what I am trying to do:
Select a is reading the Sales Order Deail table (SO_Detail) looking for any open Sales Order line item where the Sales Order line item is linked to a Job.
All of the fields will be populated except for the "WorkCenter" field which will be left blank.
Select b is reading the JOB table (JOB) looking for any "Active" Job. The Sales Order and Ln will not be populated
There is no valid PromDate from this section either.
Select c is reading the JOB_Operation (Job_Operation) for any "Active" Job. The Sales Order and Ln will not be populated.
There is no valid PromDate from this secion either.
I amy have multiple Sales Order Line Items for Sales Order Line items.
I am trying to generate out put data with the following sort sequence.
Material (which is Part_Number)
Job
SalesOrder
A Job would be selected first and then Sales Orders (I may have no Sales Orders, 1 sales order or many)
*******************************************************************************************************
Sample Output Data
*******************************************************************************************************
Material Job SalesOrder OrderDate PromDate
ABC1234567890 51110 00000 2014-05-14 (Should be Blank) from Job
ABC1234567890 51110 61234 2014-05-13 2014-05-28 from SalesOrder
ABC1234567890 51110 62345 2014-05-15 2014-05-30 from SalesOrder
BBX1234567890 52300 00000 2014-05-20 (should be blank) fron Job
CCA1234567890 52301 00000 2014-05-20 (should be blank) from Job
CCA1234567890 52303 62351 2014-05-15 2014-06-12 from SalesOrder
CCA1234567890 52310 62360 2014-05-16 2014-06-13 from SalesOrder
******************************************************************************************************
SQL Code
******************************************************************************************************
With Prod_CTE As
(
Select a.Material As [Material]
,Ma.Description As [Description]
,Ma.Lead_Days As [LD]First,
,a.SO_Line As [Ln]
,a.Job As [Job]
,a.Sales_Code AS [SalesCode]
,Sum(a.Order_Qty - a.Picked_Qty - a.Shipped_Qty) AS [OpenQty]
,Cast(SH.Order_Date As Date) AS [OrderDate]
,Cast(a.Promised_Date As Date) AS [PromDate]
,'' As [WorkCenter]
from [PRODUCTION].dbo.SO_Detail as a
inner join [PRODUCTION].dbo.Material As Ma
on a.Material = Ma.Material
inner join [PRODUCTION].dbo.SO_Header As SH
on a.Sales_Order = SH.Sales_Order
Where (a.Status = 'Open' or a.Status = 'Backorder')
and a.Job <> ''
and (a.Order_Qty - a.Picked_Qty - a.Shipped_Qty > 0)
GROUP BY a.Material, a.Job, a.Sales_Code, Ma.Lead_Days, SH.Order_Date, Ma.Description, a.Sales_Order, a.SO_Line, a.Promised_Date
UNION ALL
Select b.Part_Number As [Material]
,ISNULL(b.Description,'') As [Description]
,Mb.Lead_Days As [LD]
,'00000' As [SalesOrder]
,'' As [Ln]
,b.Job As [Job]
,Mb.Sales_Code As [SalesCode]
,Sum(b.Order_Quantity - b.Shipped_Quantity) As [OpenQty]
,Cast(b.Order_Date As Date) As [OrderDate]
,Cast(b.Order_Date As Date) As [PromDate]
,'' As [WorkCenter]
from [PRODUCTION].dbo.Job As b
inner join [PRODUCTION].dbo.Material As Mb
on b.Part_Number = Mb.Material
Where b.Status = 'Active' and Mb.Type = 'F'
GROUP BY b.Part_Number, b.Job, Mb.Sales_Code, Mb.Lead_Days, b.Order_Date, b.Description
UNION ALL
Select e.Material As [Material]
,ISNULL(e.Description,'') As [Description]
,e.Lead_Days As [LD]
,'00000' As [SalesOrder]
,'' As [Ln]
,e.Sales_Code As [SalesCode]
,'0' As [OpenQty]
,Cast(Je.Order_Date As Date) As [OrderDate]
,Cast(Je.Order_Date As Date) As [PromDate]
,Max(Case when JO.Work_Center = 'TUBEBENDER' and JO.Operation_Service = 'CUT TUBES' and JO.Status = 'O' and JO.Act_Run_Hrs = 0 then '00 NOT STARTED'
when JO.Work_Center = 'TUBEBENDER' and JO.Operation_Service = 'CUT TUBES' and Jo.Status = 'S' and JO.Act_Run_Hrs > 0 then '10 CUT TUBES'
when JO.Work_Center = 'TUBEBENDER' and JO.Operation_Service = 'CUT TUBES' and JO.Status = 'S' and JO.Act_Run_Hrs = 0 then '10 CUT TUBES'
when JO.Work_Center = 'TUBEBENDER' and JO.Operation_Service = 'BEND TUBES' and JO.Status = 'S' and JO.Act_Run_Hrs > 0 then '20 BEND TUBES'
when JO.Work_Center = 'TUBEBENDER' and JO.Operation_Service = 'BEND TUBES' and JO.Status = 'S' and JO.Act_Run_Hrs = 0 then '20 BEND TUBES'
when JO.Work_Center = 'TUBEBENDER' and JO.Operation_Service = 'CUT TUBES' and JO.Status = 'C' and JO.Act_Run_Hrs > 0 then '30 BEND TUBES'
when JO.Work_Center = 'WIREBENDER' and JO.Operation_Service = 'WIRE BEND' and JO.Status = 'S' then '80 WIRE BENDER'
when JO.Work_Center = 'FAB/WELD' and Je.Act_Total_Hrs > 0 and JO.Status = 'O' and JO.Act_Run_Hrs = 0 then '40 BENT'
when JO.Work_Center = 'FAB/WELD-A' and JO.Status = 'O' and JO.Act_Run_Hrs = 0 then '40 BENT'
when JO.Work_Center = 'ROBFABWELD' and JO.Status = 'O' and JO.Act_Run_Hrs = 0 then '40 BENT'
when JO.Work_Center = 'FAB/WELD' and JO.Status = 'S' and JO.Act_Run_Hrs > 0 then '60 WELD'
when JO.Work_Center = 'FAB/WELD-A' and JO.Status = 'S' and JO.Act_Run_Hrs > 0 then '60 WELD'
when JO.Work_Center = 'ROBFABWELD' and JO.Status = 'S' and JO.Act_Run_Hrs > 0 then '60 WELD'
when JO.Operation_Service = 'REWORK' and JO.Status = 'S' and JO.Act_Run_Hrs> 0 then '97 REWORK'
when JO.Work_Center = 'INSPECTION' and JO.Status = 'S' then '98 INSPECTION'
when JO.Work_Center = 'INSPECTION' and JO.Status = 'C' then '99 Completed'
else '' end) [WorkCenter]
,Min(Case when JO.Job is not null then JO.Job else '' end) [Job]
from [PRODUCTION].dbo.Job_Operation As JO
inner join [PRODUCTION].dbo.Job As Je
on JO.Job = Je.Job
inner join [PRODUCTION].dbo.Material As e
on e.Material = Je.Part_Number
Where e.Class <> '' and e.Class <> 'Master' and e.Status = 'Active' and e.Type = 'F' and Je.Status = 'Active'
GROUP BY e.Class, e.Material, e.Description, e.Lead_Days, e.Sales_Code, Je.Order_Date
)
Select * from Prod_CTE
--Order by COALESCE(PROD_CTE.Part_Number,PROD_CTE.Job,PROD_CTE.SalesOrder)
Error ************************************************************************************
Msg 206, Level 16, State 2, Line 1
Operand type clash: float is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
SWProduction