?
Solved

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

Posted on 2008-10-07
5
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

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

Expert Comment

by:Philip Pinnell
ID: 22659825
Is that what you want or do I misunderstand?
0
 

Author Comment

by:TiTANS_C
ID: 22660083
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 2000 total points
ID: 22660111
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
ID: 31503850
Thanks for the answer!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

762 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