Question : Exclude all records from a join which have two specific values in one table

Hi all,

I'm a SQL newbie struggling to script up a query to exclude all records from one table which have two specific values. I then want to join the results to another table to get appropriate information from it.

Table constits has several columns and the important ones are:
Sequence (Primary key)
ID (Many to one in the table I want to join)
Constit

example of constits data:
Sequence  ID  Constit
1             345   PC
2             345   STF
3             12     PC
4             23     STF
5             62     PC

Community table data
ID         Surname     GivenName
12         Bloe           Joe
23         Bloggs        Bert
345        Crun          Henry
62         Bannister     Minnie

I want to join community to constits to give me all records with a constit of PC but exclude those who have both PC and STF. So results from the above data should give me:
12      Bloe   Joe
62      Bannister  Minnie

I tried using EXCEPT but I'm still not getting the desired results.

Any help/examples greatly appreciated

Cheers
David

Answer : Exclude all records from a join which have two specific values in one table

Something like this perhaps:

SELECT      c.ID, c.Surname, c.GivenName
FROM      Community c
      INNER JOIN (
          SELECT      ID,
                MIN(Constit) Constit
          FROM      (
                SELECT      ID, Constit
                FROM      Constit
                WHERE      Constit IN ('PC', 'STF')
                GROUP BY
                      ID, Constit) a
          GROUP BY
                ID
          HAVING
                COUNT(*) = 1) b ON c.ID = b.ID
WHERE      b.Constit = 'PC'
Random Solutions  
 
programming4us programming4us