Solved

Can't figure out how to build an sqlquery with negation.

Posted on 2008-10-07
5
206 Views
Last Modified: 2010-04-21
Situation:
I'm developing a microsoft windows program in vb.net with a "search for information screen".
All data is stored in an sqldatabase and all data is about persons, contactinformation and perks.

Tables:
PERSON with columns ID, FIRSTNAME, LASTNAME, ...
(example person: 1 Joe Johnson, 2 Sam Salmon, 3 Fred Frederico
PERK with columns ID, PERKNAME, ...
(example skills: 1 Group Insurance, 2 Healt Insurance, 3 Car)
PERK_TO_PERSON with columns id, personid, perkid
Persons can have multiple perks, perks can be assigned to multiple persons.
(example: Joe can have Group and a Car. Sam can have Group and Health. Fred doesn't have a perk => so no entry for him in PERK_TO_PERSON)

Problem:
Currently all searches are pretty straightforward (select personid from perk_to_person where perkid = 2)
My boss now wants to have the ability to search on not. Example: The program must be able to extract all personsids that don't have healt insurance. In this case that would be 1 and 3.

Anyone who can help me build the querystring for that?
0
Comment
Question by:TiTANS_C
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
select personid from perk_to_person where perkid <> 2
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
Is that what you want or do I misunderstand?
0
 

Author Comment

by:TiTANS_C
Comment Utility
That is partially what i want.

The query you build will give me Joe.
But not Fred (who doesn't have an enty in perk_to_person)

The full personidlist must be checked.

(I'm so bad at explaining things, sorry if it isn't clear)

The list of entries for perk_to_person are:
id     persid     perkid  
1     1              1    
2     1              3
3     2              1
4     2              2

This is an incomplete query I build but maybe it can help you guys think in the right direction:

select distinct(persid) from perk_to_person where perkid not in (2, 3) order by persid

This gives me as result persids 1 and 2. But should give me 3. This is not possible at the moment because I'm not using the personid from table person.
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
Comment Utility
Select Person.*
From Person
Where Not exists (select PersID from Perk_to_Person Where PersID = Person.ID and Perk_To_Person.PerkID = 2)
0
 

Author Closing Comment

by:TiTANS_C
Comment Utility
Thanks for the answer!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now