Scenario: I have been asked to obtain the latest UnitPrice for the PartNum of a given LaborDtl Transaction. LaborDtl does not have a PartNum, so I join it up to JobHead using .Company and .JobNum as the links.
First things first, lets get the LaborDtl records, narrowing it down to a specific day and employee for debugging purposes. I’ve already set up the JOIN for the JobHead table and grabbed the PartNum from it as well.
SELECT
LaborDtl.Company AS LaborDtl_Company,
LaborDtl.EmployeeNum AS LaborDtl_EmployeeNum,
LaborDtl.JobNum AS LaborDtl_JobNum,
JobHead.PartNum AS JobHead_PartNum
FROM
Erp.LaborDtl AS LaborDtl
INNER JOIN Erp.JobHead AS JobHead ON LaborDtl.Company = JobHead.Company
AND LaborDtl.JobNum = JobHead.JobNum
WHERE
(
LaborDtl.ClockInDate = '01/03/2022'
AND LaborDtl.EmployeeNum = 'jamf'
)
Fairly straight forward so far. The query returns 4 rows of data:
Company | EmpNum | JobNum | PartNum |
---|---|---|---|
SA | JAMF | 1842529 | 4567890 |
SA | jamf | 1842379-CP | C123456 |
SA | JAMF | 1842379-CP | C123456 |
SA | JAMF | 1842566 | 1357924 |
That data is accurate, so time to look into the InvcDtl table to get the most recent sale price:
SELECT
TOP (1) InvcDtl.Company AS InvcDtl_Company,
InvcDtl.UnitPrice AS InvcDtl_UnitPrice,
InvcDtl.PartNum AS InvcDtl_PartNum,
InvcDtl.ShipDate AS InvcDtl_ShipDate
FROM
Erp.InvcDtl AS InvcDtl
WHERE
InvcDtl.PartNum = 'C123456'
ORDER BY
InvcDtl.ShipDate DESC
That returns:
Company | UnitPrice | PartNum | ShipDate |
---|---|---|---|
SA | 3.61000 | C123456 | 7/15/2022 |
Great! So far, so good! Time to combine them:
WITH ldQuery AS (
SELECT
LaborDtl.Company AS LaborDtl_Company,
LaborDtl.EmployeeNum AS LaborDtl_EmployeeNum,
LaborDtl.JobNum AS LaborDtl_JobNum,
JobHead.PartNum AS JobHead_PartNum
FROM
Erp.LaborDtl AS LaborDtl
LEFT OUTER JOIN Erp.JobHead AS JobHead ON LaborDtl.Company = JobHead.Company
AND LaborDtl.JobNum = JobHead.JobNum
WHERE
(
LaborDtl.ClockInDate = '01/03/2022'
AND LaborDtl.EmployeeNum = 'jamf'
)
),
idQuery AS (
SELECT
TOP (1) InvcDtl.Company AS InvcDtl_Company,
InvcDtl.UnitPrice AS InvcDtl_UnitPrice,
InvcDtl.PartNum AS InvcDtl_PartNum,
InvcDtl.ShipDate AS InvcDtl_ShipDate
FROM
Erp.InvcDtl AS InvcDtl
ORDER BY
InvcDtl.ShipDate DESC
)
SELECT
*
FROM
ldQuery
INNER JOIN idQuery ON ldQuery.LaborDtl_Company = idQuery.InvcDtl_Company
AND ldQuery.JobHead_PartNum = idQuery.InvcDtl_PartNum
That returns a rather disappointing result of:
Company | EmployeeNum | JobNum | PartNum | Company | UnitPrice | PartNum | ShipDate |
---|---|---|---|---|---|---|---|
SA | JAMF | 1842529 | 4567890 | (null) | (null) | (null) | (null) |
SA | jamf | 1842379-CP | C123456 | (null) | (null) | (null) | (null) |
SA | JAMF | 1842379-CP | C123456 | (null) | (null) | (null) | (null) |
SA | JAMF | 1842566 | 1357924 | (null) | (null) | (null) | (null) |
I tried other variations of the query, such as performing a join on the subquery itself:
LEFT OUTER JOIN (SELECT TOP (1) InvcDtl.Company.....
And I get the same results. Is there something very obvious that I am overlooking with this query? I’ve been staring at it all day, so my eyes are semi-glazed over at this point.
And I have tried this as a BAQ as well, with the same results.