Solved

SQL Server - NOT IN Help

Posted on 2008-10-09
1
176 Views
Last Modified: 2010-03-19
i need help with NOT IN, please.

table a - field name is itemID
1234-1
5678-1
0987-1

table b - field name is itemID
1234-1
5555
6666

select itemID from tableA where itemID NOT IN ( select itemID from tableB ) yields:  5678-1 and 0987-1.

why doesnt this query return any records:

select i.itemID, itemDescription from items i where inactive = 0 and
itemid NOT IN
  ( select distinct itemID from purchaseorderdetails_v2 where POnumber in ( select ponumber from purchaseorders_v2 where orderdate > '1/1/2006' ) )
order by i.itemid


this query should return over 15000 records.  if i re-write it this way, i get the desired results:

select i.itemID, itemDescription from items i left join
  ( select distinct itemID from purchaseorderdetails_v2 where POnumber in ( select ponumber from purchaseorders_v2 where orderdate > '1/1/2006' ) ) as details
ON i.itemid = details.itemid
WHERE details.itemID is null
and inactive = 0
order by i.itemid

how can i write it using the NOT IN clause, and, more importantly, what am I missing that is causing the zero recordset to be returned.  I do understand that NOT IN = <> ALL, but don't know why it seems to behave different for the statements above.

thanks.
0
Comment
Question by:freezingHot
1 Comment
 
LVL 1

Accepted Solution

by:
freezingHot earned 0 total points
Comment Utility
found the issue... the subquery was returning a null row.  the items table does not allow nulls.

once i rewrote the subquery as:

select i.itemID, itemDescription from items i where inactive = 0 and
itemid NOT IN
  ( select distinct itemID from purchaseorderdetails_v2 where POnumber in ( select ponumber from purchaseorders_v2 where orderdate > '1/1/2006' ) and itemID IS NOT NULL)

it works like a champ.
order by i.itemid
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

8 Experts available now in Live!

Get 1:1 Help Now