![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
35" in contact type 1, then put "<30 or >35" on the next 'OR' line for contact type 2, etc, etc. |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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. |
|
what this was trying to do? |
|
What is C2? |
#6
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |