Reputation: 7
I have the following query:
SELECT *
FROM ZINVN
WHERE ZINVN.GRID IN
(
SELECT SMGRID
FROM SKU FULL JOIN OTBCLS ON SKU.SKCLCD = OTBCLS.SMCLCD
WHERE SKSKU=800234
);
This query doesn't return a result, while if I run it without the in clause it returns the expected result.
Also the query inside ()
works well and returns the expected result.
Where is the problem in my query?
Upvotes: 0
Views: 98
Reputation: 107686
You should alias your query to indicate where the columns come from.
Also, an INNER JOIN will be faster than a FULL OUTER JOIN which doesn't quite make sense for two reasons
1. Having the WHERE SKU=
clause reduces the FULL JOIN to a LEFT JOIN since rows must match from the left table
2. Keeping records in the LEFT table serves no purpose since SMGRID
will be NULL for the purpose of the IN()
clause
SELECT *
FROM ZINVN
WHERE ZINVN.GRID IN
(
SELECT OTBCLS.SMGRID
FROM SKU JOIN OTBCLS ON SKU.SKCLCD = OTBCLS.SMCLCD
WHERE SKU.SKSKU=800234
);
This existential test normally executes faster when written in the equivalent form below using a correlated EXISTS test.
SELECT *
FROM ZINVN
WHERE EXISTS (
SELECT 1
FROM SKU JOIN OTBCLS ON SKU.SKCLCD = OTBCLS.SMCLCD
WHERE SKU.SKSKU=800234 AND ZINVN.GRID=OTBCLS.SMGRID
);
Having said that, have you checked for extra spaces being the difference between 'ZINVN.GRID' and 'OTBCLS.SMGRID'?
Upvotes: 3