×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

IN and NOT IN

IN and NOT IN

IN and NOT IN

(OP)
Hi,

I'm having a bit of trouble with a SELECT query.

I'm trying to select a count of the number of products which are not part of Invoices, Sales Orders etc.

The code I have so far is....

SELECT COUNT(*) FROM tbProdDesc WHERE
    [Stock Code] NOT IN
    (SELECT [Stock Code] FROM tbQuotePart UNION
     SELECT [Stock Code] FROM tbInvoicePart UNION
     SELECT [Stock Code] FROM tbSOPart UNION
     SELECT [Stock Code] FROM tbBIPart UNION
     SELECT [Stock Code] FROM tbPOPart)

However this brings back 0, when it should be over 28000.

If i change it to In, instead of NOT IN i.e.

SELECT COUNT(*) FROM tbProdDesc WHERE
    [Stock Code] IN
    (SELECT [Stock Code] FROM tbQuotePart UNION
     SELECT [Stock Code] FROM tbInvoicePart UNION
     SELECT [Stock Code] FROM tbSOPart UNION
     SELECT [Stock Code] FROM tbBIPart UNION
     SELECT [Stock Code] FROM tbPOPart)

This brings back 700 odd records which is correct.

For some reason it does not like my NOT IN command.

Any help is much apprecitaed.

Thanks

RE: IN and NOT IN

why not use a join

SELECT COUNT(*) FROM tbProdDesc p
left outer join     
(SELECT [Stock Code] FROM tbQuotePart UNION
     SELECT [Stock Code] FROM tbInvoicePart UNION
     SELECT [Stock Code] FROM tbSOPart UNION
     SELECT [Stock Code] FROM tbBIPart UNION
     SELECT [Stock Code] FROM tbPOPart) u
on p.Stock Code  = u.Stock Code
where u.stock Code is null

 

RE: IN and NOT IN

You may also try this:

CODE

SELECT COUNT(*) FROM tbProdDesc WHERE
    NOT ([Stock Code] IN
    (SELECT [Stock Code] FROM tbQuotePart UNION
     SELECT [Stock Code] FROM tbInvoicePart UNION
     SELECT [Stock Code] FROM tbSOPart UNION
     SELECT [Stock Code] FROM tbBIPart UNION
     SELECT [Stock Code] FROM tbPOPart))

BTW, does tbProdDesc has rows with NULL [Stock Code] ?

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: IN and NOT IN

(OP)
IanWaterman - Your select statement works but I need to turn it into a Delete statement, and I'm not sure of the syntax changes I would need when deleting with a joined query.

PHV - Your select statement still brings back 0, when it should be 28000 ish.  There are no rows with Null [Stock Code].

Thanks for your help.

RE: IN and NOT IN

And this ?

CODE

SELECT COUNT(*) FROM tbProdDesc
WHERE [Stock Code] NOT IN (SELECT [Stock Code] FROM tbQuotePart)
  AND [Stock Code] NOT IN (SELECT [Stock Code] FROM tbInvoicePart)
  AND [Stock Code] NOT IN (SELECT [Stock Code] FROM tbSOPart)
  AND [Stock Code] NOT IN (SELECT [Stock Code] FROM tbBIPart)
  AND [Stock Code] NOT IN (SELECT [Stock Code] FROM tbPOPart)

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: IN and NOT IN

(OP)
Still returns 0.

I managed to do it by adopting IanWaterman's code.

DELETE tbProdDesc FROM
(
    SELECT p.* FROM tbProdDesc p
    left outer join     
        (SELECT [Stock Code] FROM tbQuotePart UNION
         SELECT [Stock Code] FROM tbInvoicePart UNION
         SELECT [Stock Code] FROM tbSOPart UNION
         SELECT [Stock Code] FROM tbBIPart UNION
         SELECT [Stock Code] FROM tbPOPart) u
    on p.[Stock Code] = u.[Stock Code]
    WHERE u.[Stock Code] IS NULL)
) AS t1
WHERE tbProdDesc.[Stock Code] = t1.[Stock Code]

Thanks for all your help, it's much appreciated.

RE: IN and NOT IN

What about NULL's?

If one (or more) stock codes are NULL, the NOT IN-SELECT will return 0 rows.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close