COACHMAN99
asked on
NOT IN SQL (ms access 2013)
Hi Folks
I'm not sure why this isn't working. I need to retrieve all records except where a few IDs are input.
I tried NOT IN (0,1,2) and I get all data (no filter).
if I use NOT IN (0) the '0's are excluded (as required), same for NOT IN (1)
is it valid to use a comma-separated set of integers in the IN clause? (or just strings in quotes?)
thanks for any useful comment.
I'm not sure why this isn't working. I need to retrieve all records except where a few IDs are input.
I tried NOT IN (0,1,2) and I get all data (no filter).
if I use NOT IN (0) the '0's are excluded (as required), same for NOT IN (1)
is it valid to use a comma-separated set of integers in the IN clause? (or just strings in quotes?)
thanks for any useful comment.
ASKER
the data type is integer. see sql above (select fields form table where ID NOT IN (1,2,3)
the data type is integer
this should work
select fields form table where ID NOT IN (1,2,3)
Here the problem is not with with IN and NOT IN clause , The problem is NULLs in the subselect
Pls try ..
Pls try ..
select fields form table
where
NOT EXISTS ( SELECT NULL FROM table WHERE ID NOT IN (1,2,3) and ID IS NOT NULL )
ASKER
that's what I thought. but it doesn't. Hence the post.
ASKER
I was wondering about the null. let me try that and get back to you.
ASKER
I tried the following but now get no data?
SELECT tmpINProducts.*
FROM tmpINProducts
where NOT EXISTS ( SELECT NULL FROM tmpINProducts WHERE prodCustID NOT IN (1,2,3) and prodCustID IS NOT NULL )
ORDER BY prodCustID;
SELECT tmpINProducts.*
FROM tmpINProducts
where NOT EXISTS ( SELECT NULL FROM tmpINProducts WHERE prodCustID NOT IN (1,2,3) and prodCustID IS NOT NULL )
ORDER BY prodCustID;
here is my demo
it is very basic thing... no magic here
Products table
ID PrdName CustID
1 Mango 1
2 Pencil
3 Chair 2
4 Mousepad 0
5 Keyboard 3
SELECT *
FROM Products
WHERE CustId is null or CustID not in (0,1,2)
ID PrdName CustID
2 Pencil
5 Keyboard 3
SELECT *
FROM Products
WHERE CustID not in (0,1,2)
ID PrdName CustID
5 Keyboard 3
SELECT *
FROM Products
WHERE CustID in (0,1)
ID PrdName CustID
1 Mango 1
4 Mousepad 0
it is very basic thing... no magic here
probably you have a query like
and when it runs, it asks for CustID and you enter "0,1,2"
then query becomes
and returns nothing!!!... no customer with CustID = "0,1,2", but 0 or 1 or 2 etc...
select * from Products where CustId not in (CustList)
and when it runs, it asks for CustID and you enter "0,1,2"
then query becomes
select * from Products where CustId not in ("0,1,2")
and returns nothing!!!... no customer with CustID = "0,1,2", but 0 or 1 or 2 etc...
Pls try --
or
SELECT a.*
FROM tmpINProducts a
where NOT EXISTS ( SELECT NULL FROM tmpINProducts b WHERE prodCustID NOT IN (1,2,3) and prodCustID IS NOT NULL
AND a.prodCustID = b.prodCustID
)
ORDER BY prodCustID;
or
SELECT a.*
FROM tmpINProducts a
where NOT EXISTS ( SELECT NULL FROM tmpINProducts b WHERE prodCustID IN (1,2,3) and prodCustID IS NOT NULL
AND a.prodCustID = b.prodCustID
)
ORDER BY prodCustID;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Have you tried this-
SELECT a.*
FROM tmpINProducts a
where NOT EXISTS ( SELECT NULL FROM tmpINProducts b WHERE prodCustID IN (1,2,3) and prodCustID IS NOT NULL
AND a.prodCustID = b.prodCustID
)
ORDER BY prodCustID;
ASKER
Thanks Folks.
Pat: not sure what the full query looks like
Pawan: seems to work great -will report back.
Pat: not sure what the full query looks like
Pawan: seems to work great -will report back.
ASKER
Hi Pawan: I integrated the where-clause of your suggestion into the full query but got lost on the aliases and the join.
SELECT tmpINProducts.*, tmpItemDies.ProdID, ('P:\AA Pressed Metal\Order Entry\Product Pictures\' & Left(tmpINProducts.[ProdID ], 3) & '\' & tmpINProducts.[ProdID] & '.jpg') AS tmpPath
FROM tmpINProducts INNER JOIN tmpItemDies ON tmpINProducts.ProdID = tmpItemDies.ProdID
WHERE NOT EXISTS ( SELECT NULL FROM tmpINProducts b WHERE prodCustID IN (0, 1,2,3) and prodCustID IS NOT NULL
AND a.prodCustID = b.prodCustID)
ORDER BY prodCustID;
SELECT tmpINProducts.*, tmpItemDies.ProdID, ('P:\AA Pressed Metal\Order Entry\Product Pictures\' & Left(tmpINProducts.[ProdID
FROM tmpINProducts INNER JOIN tmpItemDies ON tmpINProducts.ProdID = tmpItemDies.ProdID
WHERE NOT EXISTS ( SELECT NULL FROM tmpINProducts b WHERE prodCustID IN (0, 1,2,3) and prodCustID IS NOT NULL
AND a.prodCustID = b.prodCustID)
ORDER BY prodCustID;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Kurt,
unfortunately we have lost all Pawan's NULL-handling.
His was very close so will wait for him to hopefully add the finishing touches.
unfortunately we have lost all Pawan's NULL-handling.
His was very close so will wait for him to hopefully add the finishing touches.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what you should do is this
create excel and put this, so everybody knows what you have, what is needed...
EE-HowToAsk.xlsx
create excel and put this, so everybody knows what you have, what is needed...
EE-HowToAsk.xlsx
ASKER
Thanks Pawan, that seems to work. I'll check more closely tomorrow
Thanks also to all who participated
Thanks also to all who participated
how's your SQL looks like?