?
Solved

SQL SELECT statement opposite to the one here.

Posted on 2004-04-28
7
Medium Priority
?
575 Views
Last Modified: 2008-01-09
Apologies for the poor question title.

I have three tables:

Tbl_AddressBook has these fields:
ContactID (Autonumber)
FirstName (text e.g. Alex)
LastName (text e.g. Harris)

Tbl_Groups has these fields:
GroupID (Autonumber)
GroupName (text e.g. Clients)

Tbl_GroupLinks has these fields:
LinkID (Autonumber)
ContactID (Number)
GroupID (Number)

Each member of the address book may or may not belong to a group.  They may belong to many groups, but only belong to the same group once.

I have created the following query to display the members of a particular group:

SELECT Tbl_Groups.GroupName, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks INNER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID) INNER JOIN Tbl_Groups ON Tbl_GroupLinks.GroupID=Tbl_Groups.GroupID
WHERE Tbl_GroupLinks.GroupID=1;

What I require is the opposite of this, a query which displays all the members of Tbl_AddressBook that are not in Tbl_GroupLinks.GroupID=1.

Please help me to solve this.  I am sure that for an expert this is relatively simple.

Regards

Alex Harris
0
Comment
Question by:videnda
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:ljian
ID: 10939343
SELECT Tbl_Groups.GroupName, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks INNER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID) INNER JOIN Tbl_Groups ON Tbl_GroupLinks.GroupID=Tbl_Groups.GroupID
WHERE not Tbl_GroupLinks.GroupID=1;
0
 

Author Comment

by:videnda
ID: 10939628
Not exactly what I am looking for ljian.  Your statement only returns a contact if they are a member of a group.  Also if a contact appears in multiple groups, it still displays the contact with your statement.  My raw data is:

ContactID FirstName LastName GroupName
30 Adam Threadgill
31 Alasdair Macdonald
32 Becky Simmonds Group1
33 Ben Looijenga Group2
34 Ben Yelton Group3
34 Ben Yelton Group1
35 Calli Tofts Group1
36 Christy Nevin Group1
37 Chris Dingley Group4
38 Claire Ford Group2
39 Dahlia Pereria
41 David George MacIntosh
42 Emma Stinton
43 Helen Guy-Mas
44 Jackie Cordy
45 James Dundas
46 James Ford Group3
46 James Ford Group2
46 James Ford Group1
47 Janet Willing
48 Johnny Quarrie
49 John Vickers
50 Jon Bowyer Group3
50 Jon Bowyer Group1

If I run my first query WHERE Tbl_GroupLinks.GroupID=1, I am returned:
ContactID FirstName LastName GroupName
32 Becky Simmonds Group1
34 Ben Yelton Group1
35 Calli Tofts Group1
36 Christy Nevin Group1
46 James Ford Group1
50 Jon Bowyer Group1

The solution I require, will display the remaining contacts e.g.:
ContactID FirstName LastName GroupName
30 Adam Threadgill
31 Alasdair Macdonald
33 Ben Looijenga Group2
37 Chris Dingley Group4
38 Claire Ford Group2
39 Dahlia Pereria
41 David George MacIntosh
42 Emma Stinton
43 Helen Guy-Mas
44 Jackie Cordy
45 James Dundas
47 Janet Willing
48 Johnny Quarrie
49 John Vickers

Note the following do not appear in the second list even though they are in other groups:
34 Ben Yelton Group1
46 James Ford Group1
50 Jon Bowyer Group1

I hope this clarifies what I meant by opposite.

Alex
0
 
LVL 2

Expert Comment

by:ljian
ID: 10940336
In this case you should use left (right ) outer join instead of inner join in the from clause which retrieves only the group members ( strict equivalence Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID )
I think this works! Try with RIGHT if LEFT doesn't work
SELECT Tbl_GroupLinks.GroupID, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks LEFT OUTER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID)
WHERE not Tbl_Groups.GroupID=1;

I don't see the point for the groups table though.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:videnda
ID: 10940571
That is closer to what I am looking for and returns all the Contacts not in the first query.  But it also returns contacts from the first query who are in another group.

I think I need a subquery or something similar to the following.  The syntax is probably wrong because I am an SQL amateur.  Perhaps you can help:

Select Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM Tbl_AddressBook
WHERE Tbl_AddressBook.ContactID NOT IN(SELECT Tbl_Groups.GroupName, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks INNER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID) INNER JOIN Tbl_Groups ON Tbl_GroupLinks.GroupID=Tbl_Groups.GroupID
WHERE Tbl_GroupLinks.GroupID=1);

Probably completely wrong but I am trying!!

Your help is appreciated.

Alex
0
 
LVL 2

Accepted Solution

by:
ljian earned 1000 total points
ID: 10940725
yes ... but if you compare contactid's and the result of the subquery should return a set of contactid's
Try this:
 
Select Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM Tbl_AddressBook
WHERE Tbl_AddressBook.ContactID NOT IN(
           SELECT Tbl_GroupsLinks.ContactID
           FROM Tbl_GroupLinks
           WHERE Tbl_GroupLinks.GroupID=1);
0
 

Author Comment

by:videnda
ID: 10940780
It worked.  Thank you for your very fast replies.

Alex
0
 
LVL 2

Expert Comment

by:ljian
ID: 10940796
I'm glad I could help!

Lucian
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
What we learned in Webroot's webinar on multi-vector protection.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

829 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