Solved

Problem with querying record with multiple records in a joined table

Posted on 2009-05-13
2
187 Views
Last Modified: 2012-05-06
We have two tables, Names and Prefs. Prefs have fields ID (key back to Names), method and request. This is how we track the Do Not Contact lists. Someone can have method = 'Fax' and request = 'Opt-Out' and when we pull a query for fax, this person will be skipped.

The problem is a person may have more than one Prefs record for other requests like Opt-Out of Email and Opt-In Phone etc. So for same person in Names table there may be mulitple Prefs table records.

So, when we set up a SQL query joining the Names and Prefs tables together by ID, we can put in a WHERE statement of method<>'Fax' and request<>'Opt-Out' but it will still pull the Names record if there is another entry in the Prefs table.

So, not sure how to word this question. But how do we pull a query with 2 tables (Names and Prefs) where we don't want the Names record if it has specific record in Prefs table (i.e. Opt-Out of Fax), barring all other Prefs records attached to that Names record?

Thanks in advance.
0
Comment
Question by:mhuang121
2 Comments
 
LVL 4

Accepted Solution

by:
joeyw earned 500 total points
ID: 24374418
Try this:

Select * from names as A
where id not in (select id from prefs as b where a.id=b.id and  method='Fax' and request='Opt-Out' )
0
 

Author Closing Comment

by:mhuang121
ID: 31580952
Thank a lot for the solution! I was trying to use the not in function but forgot the id=id check in the subquery. Thanks again!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question