January 20, 2010 at 3:32 pm
Hi all,
I'm a 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
January 20, 2010 at 4:26 pm
Hope this what you're looking for:
select surname, givenname
from community as cm join constits as cn on cm.id = cn.id
where cn.constit = 'PC'
and not exists ( select constits.id from
constits where constits.id = cn.id
and constits.constit <> 'PC' )
More accurately for your requirement substitute = 'STF' for <> 'PC'.
Have fun.
January 20, 2010 at 5:06 pm
Sorry Andrew,
that query returned no results at all.
One acperkins submitted the following which does work:
SELECT c.ID, c.Surname, c.GivenName
FROM Community c
INNER JOIN (
SELECT ID,
MIN(Constit) Constit
FROM (
SELECT ID, Constit
FROM Constits
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'
And whilst I dont fully understand it, it does the job perfectly so I'm happy.
Cheers
David
January 20, 2010 at 5:50 pm
tasdavid (1/20/2010)
And whilst I dont fully understand it, it does the job perfectly so I'm happy.
Might I recommend that until you do understand what this code is doing, that you do not just move it into any production environment. If you don't understand it, then you will never know if any problem you may run into is due to this code or not.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 20, 2010 at 6:28 pm
Thanks for the sound advice Wayne,
I'm only using it to extract data for something else outside of the database. It will never be used to inject data back in and once I get a handle on what it does I will also feel a lot happier.
As I say, I'm new to SQL and learning all the time.
Cheers
David
January 21, 2010 at 8:56 am
Hi David,
I'm not sure who Andrew is, but I'm baffled as to why the code I posted didn't work since it was a cut and paste from a working query that gave your intended results, excepting the id column.
This was my first attempt at a reply and it highlights the standing recommendation to include as much information as possible - in this case a complete script for the table build, population, and query in the post.
Glad you have a solution!
richard
January 21, 2010 at 3:58 pm
Oops my apologies Richard,
Too much assaulting my brain when I replied.
I've just retried your code as is using <> 'PC' (I used <> 'STF' when I got no results) and this time I get results but many of the records I want to see are missing. Not sure why but when I have a moment I'll investigate further.
The other code I got does return all the results I want so I'll stick with that.
Many thanks for all your help. Its greatly appreciated.
Cheers
David
January 21, 2010 at 4:41 pm
Hello again Richard,
I re-examined the logic of what you submitted and realised the last line is the key.
By changing
and constits.constit <> 'STF'
to
and constits.constit = 'STF'
I get the results I wanted and the beauty is that I understand the logic. This works because we are testing against NOT EXISTS.
So the full code becomes:
SELECT
cm.ID,
cm.Surname,
cm.Preferred,
cn.Constit
FROM Community AS cm JOIN Constits AS cn ON cm.ID = cn.ID
WHERE cn.Constit = 'PC'
AND NOT EXISTS (
SELECT Constits.id
FROM Constits
WHERE Constits.id = cn.id
AND Constits.Constit = 'STF' )
ORDER BY cm.Surname, cm.Preferred;
Once again Many thanks for your assistance
Cheers
David
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply