469,613 Members | 1,354 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Access query to include records WITHOUT a certain value

Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon

Sep 11 '06 #1
5 2320
could you: first write a query to find all contacts with contact type
30 - 35 and then use the results fo that query as an 'excluding query'
to select all contacts except those in the excluding query?

Alternatively, can you not use OR in the QBE form, that is put "<30 or
>35" in contact type 1, then put "<30 or >35" on the next 'OR' line for contact type 2, etc, etc.
DG.

Sep 11 '06 #2
redstamp wrote:
Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon
(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin
Sep 11 '06 #3

Smartin wrote:
redstamp wrote:
Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon

(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin
Thanks for that Smartin, just wondered, what this was trying to do, do
I can insert names where appropriate, i.e. what is C2 and don't I need
to pull * from Customer, not Contacts?

Cheers in advance for your help.

Jon.

Sep 12 '06 #4
redstamp wrote:
Smartin wrote:
>redstamp wrote:
>>Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon
(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin

Thanks for that Smartin, just wondered, what this was trying to do, do
I can insert names where appropriate, i.e. what is C2 and don't I need
to pull * from Customer, not Contacts?

Cheers in advance for your help.

Jon.
My bad, I was returning CONTACTS records, you wanted CUSTOMERS.

SELECT *
FROM CUSTOMERS
WHERE CUSTOMERS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;
what this was trying to do?
As suggested by another poster, the technique to find "What X are not in
Y" is sometimes solved by taking an inverse approach: Determine what X
/are in/ Y, and exclude those from the results.

In this case, the subquery builds a list of CONTACTS who have at least
one contact type between 30 and 35. The outer query says, give me all
the customers that do NOT appear in the subquery.

This sort of thing can also be done using JOINs. I find the subquery to
be easier to read, though IIRC the JOIN construct is more efficient. In
a small table (a few 1000's of rows) my guess is it won't make any
noticeable difference.
What is C2?
C2 is an alias for the CONTACTS table. The alias accomplishes two
things. One, since I chose a very short name it makes it a little easier
to refer to CONTACTS fields. More importantly in the query I posted
first the alias distinguishes between two instances of the CONTACTS
table. For this reason the alias would be required in the first query.
It is optional in the second.

Hope this helps!

--
Smartin
Sep 12 '06 #5
Thank you very much, this was most helpful indeed!
Smartin wrote:
redstamp wrote:
Smartin wrote:
redstamp wrote:
Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon

(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin
Thanks for that Smartin, just wondered, what this was trying to do, do
I can insert names where appropriate, i.e. what is C2 and don't I need
to pull * from Customer, not Contacts?

Cheers in advance for your help.

Jon.

My bad, I was returning CONTACTS records, you wanted CUSTOMERS.

SELECT *
FROM CUSTOMERS
WHERE CUSTOMERS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;
what this was trying to do?

As suggested by another poster, the technique to find "What X are not in
Y" is sometimes solved by taking an inverse approach: Determine what X
/are in/ Y, and exclude those from the results.

In this case, the subquery builds a list of CONTACTS who have at least
one contact type between 30 and 35. The outer query says, give me all
the customers that do NOT appear in the subquery.

This sort of thing can also be done using JOINs. I find the subquery to
be easier to read, though IIRC the JOIN construct is more efficient. In
a small table (a few 1000's of rows) my guess is it won't make any
noticeable difference.
What is C2?

C2 is an alias for the CONTACTS table. The alias accomplishes two
things. One, since I chose a very short name it makes it a little easier
to refer to CONTACTS fields. More importantly in the query I posted
first the alias distinguishes between two instances of the CONTACTS
table. For this reason the alias would be required in the first query.
It is optional in the second.

Hope this helps!

--
Smartin
Sep 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.