Need to find records in subinv that do NOT have a listing in subinv table with substatus 1
when there is a listing in inv with status 1.
There is only one record per part number in inv, but can be several records in subinv with one of several status numbers. My statement returns no rows, shows no errors, but there are records that qualify.
SELECT m.partnum
FROM inv m,
subinv s
WHERE m.status = '1'
AND not exists (SELECT s.partnum
FROM subinv s1
WHERE s1.substatus = '1')
You have a Cartesian product in your outer query, which is not good (especially as you don’t want that join anyway), and you need to specify the part number in the EXISTS query:
This features a correlated sub-query for the NOT EXISTS. It would be worth checking the performance of a filtered OUTER JOIN:
The LEFT JOIN condition will generate a NULL for
s.substatuswhen there is no row withs.substatus = '1', and the WHERE clause only selects such rows. Not blindingly obvious as a transform, but it might be faster than the sub-query simply because it is a join rather than a sub-query that has to be executed for each row in the maininvtable (that satisfiesstatus = '1').