Hello I have a stored porceedure below. I'm not getting results as expected. Basically I want every record from the OpenJob (O)table returned that matches the Where clause, then additional information when not empty from the other tables. If the other tables are empty for that job number, I don't get a result at all.
I've tried multiple combinations of Outer Joins, Distinct O.JobN, I've even created a view containing the Where statement and queried that.
What am I missing.
Thanks in advance for your time.
CREATE PROCEDURE [dbo].[Pplan_full] AS
Select O.JobN,
O.CustomerN,
O.Quantity,
O.DueDate,
O.JobDescription,
O.ProdPlanner,
J.StatusCode,
J.Note,
J.TransactionN,
J.Time,
MaxDateTable.LastDate,
JL.UpdateDate,
LaborCostCntr.LCCDescription,
JL.LLRecNum,
JL.StartTime
From OpenJob AS O
Left Outer Join JobStatus1 AS J
on O.JobN = J.JobN
Join JobLabor AS JL
on J.JobN = JL.JobN
Join (
Select JobN, Max(TransactionN) As MaxTransactionN
From JobStatus1
Group By JobN
) As MaxTable
On J.JobN = MaxTable.JobN
And J.TransactionN = MaxTable.MaxTransactionN
Join (
Select JobN, Max(LLRecNum) as LastDate
From JobLabor
Group By JobN
) As MaxDateTable
On JL.LLRecNum = MaxDateTable.LastDate
and JL.JobN = MaxDateTable.JobN
Join LaborCostCntr
On LaborCostCntr.LCCN = JL.LCCN
Where (O.JobStatus = '0' or O.JobStatus = '2' )
and O.DueDate BETWEEN dateadd(day,-7,getdate()) and dateadd(day,14,getdate())
and O.JobDescription not like 'WF%'
GO
I've tried multiple combinations of Outer Joins, Distinct O.JobN, I've even created a view containing the Where statement and queried that.
What am I missing.
Thanks in advance for your time.
CREATE PROCEDURE [dbo].[Pplan_full] AS
Select O.JobN,
O.CustomerN,
O.Quantity,
O.DueDate,
O.JobDescription,
O.ProdPlanner,
J.StatusCode,
J.Note,
J.TransactionN,
J.Time,
MaxDateTable.LastDate,
JL.UpdateDate,
LaborCostCntr.LCCDescription,
JL.LLRecNum,
JL.StartTime
From OpenJob AS O
Left Outer Join JobStatus1 AS J
on O.JobN = J.JobN
Join JobLabor AS JL
on J.JobN = JL.JobN
Join (
Select JobN, Max(TransactionN) As MaxTransactionN
From JobStatus1
Group By JobN
) As MaxTable
On J.JobN = MaxTable.JobN
And J.TransactionN = MaxTable.MaxTransactionN
Join (
Select JobN, Max(LLRecNum) as LastDate
From JobLabor
Group By JobN
) As MaxDateTable
On JL.LLRecNum = MaxDateTable.LastDate
and JL.JobN = MaxDateTable.JobN
Join LaborCostCntr
On LaborCostCntr.LCCN = JL.LCCN
Where (O.JobStatus = '0' or O.JobStatus = '2' )
and O.DueDate BETWEEN dateadd(day,-7,getdate()) and dateadd(day,14,getdate())
and O.JobDescription not like 'WF%'
GO