We help IT Professionals succeed at work.
Get Started

"NOT IN" Query Returns No Rows

166 Views
Last Modified: 2014-05-02
I have been playing with this query for some time now and can't figure out what I am missing. It is probably something simple but I can't for the life of me see it. I have a query in which I am simply trying to get the a list of values that are not in another list. I am using CTEs as much of this is sub-querying the same view.

The query looks like this:

;WITH CTE_NONULLS AS (
SELECT MAX(CabinetID) AS CabinetID, TIN FROM [Trades].[dbo].[vw_CabinetNG_New] 
WHERE (Source = 'DST' OR Source = 'DAZL') 
  AND (Address1 IS NOT NULL) AND (TIN IS NOT NULL AND TIN <> '000-00-0000')
  GROUP BY TIN),
CTE_NULLS AS (
SELECT MAX(CabinetID) AS CABINETID, FirstName, TIN FROM [Trades].[dbo].[vw_CabinetNG_New]
WHERE (Source = 'DST' OR Source = 'DAZL') 
  AND (Address1 IS NOT NULL) AND ((FIRSTNAME IS NOT NULL AND FIRSTNAME <> '') OR (LASTNAME IS NOT NULL AND LASTNAME <> ''))
  AND (TIN IS NULL OR TIN = '000-00-0000')
  GROUP BY FirstName, TIN),
CTE_FC AS (
SELECT TIN FROM [dbo].[vw_CabinetNG_New] 
WHERE Source = 'FirstClearing'
GROUP BY TIN
)
SELECT TIN
	  , CASE WHEN [FirstName] IS NOT NULL THEN [FirstName] + ' ' ELSE '' END
	   + CASE WHEN [LastName] IS NOT NULL THEN [LastName] ELSE '' END  
	   AS [Full Name]
	  ,CASE WHEN [AdditionalName] IS NULL THEN '' ELSE [AdditionalName] END AS [AdditionalName]
      ,CASE WHEN [Address1] IS NULL THEN '' ELSE [Address1] END AS [Address1]
      ,CASE WHEN [Address2] IS NULL THEN '' ELSE [Address2] END AS [Address2]
      ,CASE WHEN [Address3] IS NULL THEN '' ELSE [Address3] END AS [Address3]
      ,[City]
      ,[State]
      ,[ZipCode]
	  ,LastUpdated
	  ,[Source]
FROM [Trades].[dbo].[vw_CabinetNG_New] 
WHERE CabinetID IN
(SELECT CabinetID FROM CTE_NONULLS UNION ALL SELECT CabinetID FROM CTE_NULLS)
AND TIN NOT IN (SELECT TIN FROM CTE_FC) 
ORDER BY TIN

Open in new window


Pretty simple. If I end the query at the UNION ALL join and leave out the "AND TIN NOT IN...." I get about 5100 records. If I add the "AND TIN NOT IN...." I get 0 records. However, if I change the "NOT IN" to "IN" I get about 2300 matches, which is what it should be. So what happens to my 2800 records that do not match when I use the "NOT IN" instead of the "IN"?

Like I said it is probably going to be something crazy simple but I am stumped.

Thanks ahead of time for you help.
Comment
Watch Question
Associate Principal Engineer
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE