Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

no Sql data returned even though it exists

Here is my sqlquery :

SELECT *
FROM work_orders
WHERE LineKey = 'ABC' 
AND StateKey  = 'Approved' 
AND orderKey = 'Released' 
AND WOID NOT IN (
    SELECT WOID 
    FROM serialized_assembly
    GROUP BY (WOID)
    HAVING COUNT(WOID) > 9
    )   
LIMIT 1

Which does not return any data even though there’s an WOID 1234 that passes all these conditions.

When I break this query into two parts:
part1)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT *
FROM work_orders
WHERE LineKey = 'ABC' AND StateKey  = 'Approved' AND orderKey = 'Released'

I can see WOID 1234 in my results

part2)

SELECT WOID 
FROM serialized_assembly
GROUP BY (WOID)
HAVING COUNT(WOID) > 9

I can see that WOID 1234 is not a part of that group in the results

However, when I combine those two, I get nothing. Is there anything wrong with the sql query ?

>Solution :

NOT IN (null) is probably the problem here, because that will never be TRUE.

You can use NOT EXISTS instead, or compare the count:

SELECT *
FROM work_orders
WHERE LineKey = 'ABC' 
AND StateKey  = 'Approved' 
AND orderKey = 'Released' 
AND (SELECT COUNT(WOID)
     FROM serialized_assembly
     WHERE work_orders.WOID = serialized_assembly.WOID) <= 9
LIMIT 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading