简体   繁体   中英

Exclude entire row based on based on values from another query

I am using MS Access and I have a rather complex situation.

I have Respondents who are linked to varying numbers of different Companies via 2 connecting tables. I want to be able to create a list of distinct customers which excludes any customer associated with Company X.

Here is a pic of the relationships that are involved with the query.

And here is an example of what I'm trying to achieve.

RespondentRef | Respondent Name
8               Joe Bloggs

.

RespondentRef | GroupRef
8               2

.

GroupRef | CompanyRef
2          10

.

CompanyRef | CompanyName
10           Ball of String

I want a query where I enter in 'Ball of String' for the company name, and then it produces a list of all the Respondents (taken from Tbl_Respondent) which completely excludes Respondent 8 (as he is linked to CompanyName: Ball of String).

     Tbl_Respondent
RespondentRef | Respondent Name
...             ...
7               Bob Carlyle
9               Anton Boyle

I have tried many combinations of subqueries with <> and NOT EXISTS and NOT IN and nothing seems to work. I suspect the way these tables are linked may have something to do with it.

Any help you could offer would be very much appreciated. If you have any questions let me know. (I have made best efforts, but please accept my apologies for any formatting conventions or etiquette faux-pas I may have committed.)

Thank you very much.

EDIT:

My formatted version of Frazz's code is still turning resulting in a syntax error. Any help would be appreciated.

SELECT *
FROM Tbl_Respondent
WHERE RespondentRef NOT IN (
  SELECT tbl_Group_Details_Respondents.RespondentRef
  FROM tbl_Group_Details_Respondents
  JOIN tbl_Group_Details ON tbl_Group_Details.GroupReference = tbl_Group_Details_Respondents.GroupReference
  JOIN tbl_Company_Details ON tbl_Company_Details.CompanyReference = tbl_Group_Details.CompanyReference
  WHERE tbl_Company_Details.CompanyName = "Ball of String"

)

This should do what you need:

SELECT *
FROM Tbl_Respondent
WHERE RespondentRef NOT IN (
  SELECT gdr.RespondentRef
  FROM Tbl_Group_Details_Respondent gdr
  JOIN Tbl_Group_Details            gd  ON gd.GroupRef=gdr.GroupRef
  JOIN Tbl_Company_Details          cd  ON cd.CompanyRef=gd.CompanyRef
  WHERE cd.CompanyName='Ball of String'
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM