Solved

SQL exist query

Posted on 2012-04-10
6
331 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now