Solved

SQL exist query

Posted on 2012-04-10
6
332 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Suggested Solutions

Title # Comments Views Activity
Webservices in T-SQL 3 31
performance query 4 24
sqlserver get datetime field and create a string 5 18
abs operation in sql server 15 15
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

810 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