Solved

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

Posted on 2008-10-07
5
249 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 500 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

726 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