In your query, the values from t2
and t3
can be NULL
. These automatially fail the where
clause.
The solution is to move these conditions into the on
clause:
SELECT t1.ITEM, t1.DESCRIPTION, t1.CLASS, t1.PACK1, t1.PACK2, t1.PACK3,
t2.PACK1, t2. PACK2, t2.PACK3, SUM(t3.QUANTITY) as QOH
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.CLASS = t2.CLASS and t2.pack2 <> 'LB' and t2.pack3 <> 'LB' LEFT JOIN
Table3 t3
ON t1.ITEM = t3.ITEM
WHERE t1.PACK2 != 'LB') AND (t1.PACK3 != 'LB')
GROUP BY t1.ITEM, t1.DESCRIPTION, t1.CLASS, t1.PACK1, t1.PACK2, t1.PACK3, t2.PACK1, t2. PACK2, t2.PACK3
ORDER BY t1.ITEM;
EDIT:
This is a complicated condition. I think you want a left outer join. Then you want to check if the join fails or that a match is not to LB
:
SELECT t1.ITEM, t1.DESCRIPTION, t1.CLASS, t1.PACK1, t1.PACK2, t1.PACK3,
t2.PACK1, t2. PACK2, t2.PACK3, SUM(t3.QUANTITY) as QOH
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.CLASS = t2.CLASS LEFT JOIN
Table3 t3
ON t1.ITEM = t3.ITEM
WHERE (t1.PACK2 <> 'LB') AND (t1.PACK3 <> 'LB') AND
(t2.PACK2 <> 'LB' or t2.PACK2 is null) AND (t2.PACK3 <> 'LB' or t2.PACK3 is NULL)
GROUP BY t1.ITEM, t1.DESCRIPTION, t1.CLASS, t1.PACK1, t1.PACK2, t1.PACK3,
t2.PACK1, t2. PACK2, t2.PACK3
ORDER BY t1.ITEM;