?
Solved

SQL NOT IN

Posted on 2012-03-23
1
Medium Priority
?
230 Views
Last Modified: 2012-06-22
I need to SELECT claims from b_claim table based on data  I have data for the epccc table.


select b_claim.epcccId, * from b_claim INNER JOIN epccc ON epcccId =epccc.Id where b_claim.agencyId = 18 and epcccEpId = 25363
and epcccId  NOT IN (select id from epccc where epcccEpId = 25363
and agencyId = 18 and coalesce(epcccCccId,0) not in (-0,408))


The above works good when

(select id from epccc where epcccEpId = 25363
and agencyId = 18 and coalesce(epcccCccId,0) not in (-0,408))

returns at least one row but when it returns nothing the whole query returns everything.  

I would like it if the NOT IN portion returns nothing then I want the whole query to return nothing.  How do I do this?
0
Comment
Question by:huerita37
1 Comment
 
LVL 6

Accepted Solution

by:
ingriT earned 2000 total points
ID: 37757413
Not the prettiest way;

select b_claim.epcccId, * from b_claim INNER JOIN epccc ON epcccId =epccc.Id where b_claim.agencyId = 18 and epcccEpId = 25363
and epcccId  NOT IN (select id from epccc where epcccEpId = 25363
and agencyId = 18 and coalesce(epcccCccId,0) not in (-0,408))
AND NOT ((select Count(id) from epccc where epcccEpId = 25363
and agencyId = 18 and coalesce(epcccCccId,0) not in (-0,408)) = 0)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

850 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