Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-10-07
5
Medium Priority
?
263 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

610 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