Solved

SQL exist query

Posted on 2012-04-10
6
330 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
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

21 Experts available now in Live!

Get 1:1 Help Now