Here is my situation.
PayTable
+-------+------+--------------+
| Craft | Job | sequence |
+-------+------+--------------+
| 400 | 1 | 1 |
+-------+------+--------------+
| 401 | 2 | 2 |
+-------+------+--------------+
| 5501 | 2 | 3 |
+-------+------+--------------+
Jobs
+-------+------+------+
| Job | CraftTemplate |
+-------+------+------+
| 1 | 1 |
+-------+------+------+
| 2 | 1 |
+-------+------+------+
Pay Template
+-------+--+
| Template |
+-------+--+
| 1 |
+-------+--+
PayCraftTemplate
+-------+------+---------+
| PayTemplate | Craft |
+-------+------+---------+
| 1 | 400 |
+-------+------+---------+
| 1 | 401 |
+-------+------+---------+
What I need to do is find all crafts from the PayTable where the craft does not exist in the PayCraftTemplate. This seems pretty straight forward as an anti join pattern, but I cannot seem to get the data to return back appropriately.
The Join Links are:
PayTable INNER JOIN Jobs by Job -> Job
Jobs LEFT OUTER JOIN Pay Template by CraftTemplate -> Template
Pay Template LEFT OUTER JOIN by Template -> PayTemplate
Here is my current attempt:
select
*
FROM
PayTable
WHERE NOT EXISTS (
SELECT 1
FROM
Jobs
LEFT OUTER JOIN PayTemplate
ON PayTemplate.Template = Jobs.CraftTemplate
LEFT OUTER JOIN PayCraftTemplate
ON PayCraftTemplate.Template = PayTemplate.Template
WHERE
PayTable.Craft = PayCraftTemplate.Craft AND PayTable.Job = Jobs.Job
) AND PayTable.Job IS NOT NULL AND PayTable.Craft IS NOT NULL
This is not returning the data I expect, I would expect row 3 of PayTable to only return instead I am getting rows 1,2