Solved

SQL NOT IN

Posted on 2012-03-23
1
219 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 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Datetime function 12 81
calculated column 12 74
Error when loading the database 16 54
Sql Query Datatype 2 14
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

943 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

5 Experts available now in Live!

Get 1:1 Help Now