Solved

SQL exist query

Posted on 2012-04-10
6
335 Views
Last Modified: 2012-06-27
Snapshot of data:
num                               type                       f_id
7654321                           1                          7
7654321                           1                         2
7654321                           70                         3
1234567                           1                          2
1234567                           70                         3
1234567                           70                         2

I need the sql query that returns the num when the num is not associated to f_id = 7:
In this example: it should return 1234567
0
Comment
Question by:needplease
[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
6 Comments
 
LVL 11

Expert Comment

by:Simone B
ID: 37829992
If I understand correctly what you're looking for, this should work:

SELECT num FROM tbl WHERE num NOT in
(SELECT num FROM tbl
WHERE f_id = 7)
0
 
LVL 11

Expert Comment

by:Simone B
ID: 37829998
Actually, you should probably add DISTINCT to the previous query:

SELECT DISTINCT num FROM tbl WHERE num NOT in
(SELECT num FROM tbl
WHERE f_id = 7)
0
 

Author Comment

by:needplease
ID: 37830768
The query still returns both numbers:

7654321
1234567
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 18

Expert Comment

by:deighton
ID: 37831977
what does this return?

SELECT distinct num FROM tbl
WHERE f_id = 7

1234567 - should not be in there, if it is, then that explains it

TRY

SELECT DISTINCT tbl.num FROM tbl WHERE NOT EXISTS
(SELECT 0 FROM tbl  n2
WHERE n2.f_id = 7 AND n2.num = tbl.num)
0
 
LVL 4

Accepted Solution

by:
agusacil earned 500 total points
ID: 37832353
This should get what you need :

select distinct(num) from tablename t1 where not exists (select num from tablename t2 where t2.ID = 7 and t2.num = t1.num)

Open in new window

0
 

Author Closing Comment

by:needplease
ID: 37832878
Perfect
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

739 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