Solved

How can I get this "NOT IN" to work?

Posted on 2016-10-25
5
30 Views
Last Modified: 2016-10-27
Here's a SELECT statement that retrieves all of the customers (under the heading of PracticeID=194) who have made a payment within the last 30 days:

SELECT p.*
  FROM [PCAR_Data].[dbo].[Payment] p
  WHERE
  AccountID in (
  SELECT AccountID from Account a where a.PracticeID=194
  AND a.AccountStatusCode = 'AC' OR a.AccountStatusCode='IN' OR a.AccountStatusCode='PP'
  )
  AND p.PaymentPostedDate > DATEADD(DAY, -30, CAST(GETDATE() AS date))
  ORDER by p.PaymentPostedDate DESC 

Open in new window


Perfect!

Now, here's the SELECT statement that retrieves all of the Accounts that fall under the heading of PracticeID=194, that are current, by virtue of the AccountStatusCode being AC, PP, or IN:

SELECT  ac.*
FROM [PCAR_Data].[dbo].[Account] ac 
WHERE
ac.AccountStatusCode = 'AC' 
OR 
ac.AccountStatusCode='IN' 
OR 
ac.AccountStatusCode='PP'
AND 
ac.PracticeID=194

Open in new window


I want to put these two SELECT statements together in a way where I'm getting all the Accounts from the Account table that are NOT IN the table that has all of the accounts that have made a payment. In other words, who hasn't made a payment...?

I tried NOT EXISTS and it looked like this:

SELECT  ac.*
FROM [PCAR_Data].[dbo].[Account] ac 
WHERE
ac.AccountStatusCode = 'AC' 
OR 
ac.AccountStatusCode='IN' 
OR 
ac.AccountStatusCode='PP'
AND 
ac.PracticeID=194
AND
NOT EXISTS

(
SELECT p.*
FROM [PCAR_Data].[dbo].[Payment] p
WHERE
	AccountID in (
	SELECT AccountID from Account a where a.PracticeID=194
	)
AND 
p.PaymentPostedDate > DATEADD(DAY, -30, CAST(GETDATE() AS date))
)

Open in new window


There are not errors, but I'm getting a huge recordset that includes accounts that are limited to PracticeID=194.

What am I doing wrong?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 41858854
Well first check, is your where conditions are probably wrong, you should be bracketing the "or" statements.  As it is, the effect is all accounts where code = 'AC', a,, accounts where code = 'IN', and all accounts where both code = 'PP' and practice = 194.

I'm guessing you mean:

SELECT  ac.*
FROM [PCAR_Data].[dbo].[Account] ac 
WHERE
(
	ac.AccountStatusCode = 'AC' 
	OR 
	ac.AccountStatusCode='IN' 
	OR 
	ac.AccountStatusCode='PP'
)
AND ac.PracticeID=194

Open in new window


Or optionally:

SELECT  ac.*
FROM [PCAR_Data].[dbo].[Account] ac 
WHERE ac.AccountStatusCode in ('AC', 'IN', 'PP')
AND ac.PracticeID=194

Open in new window

0
 

Author Comment

by:brucegust
ID: 41859054
Snarf!

We're cooking with grease! This helped in a big way! I went from from 20,000 rows to 5K, so we're making progress.

Thing is, when I combine the change with the rest of the code, I don't get any rows, so I'm still missing something somewhere.

Right now, I'm using a "NOT EXISTS" because I got an error when I tried "NOT IN." Bottom line: I want every account id from the Account table that is NOT IN the recordset defined by:

SELECT p.*
FROM [PCAR_Data].[dbo].[Payment] p
WHERE
      AccountID in (
      SELECT AccountID from Account a where a.PracticeID=194
      )
AND
p.PaymentPostedDate > DATEADD(DAY, -30, CAST(GETDATE() AS date))

I've got over 5K with the SELECT statement you just helped me with and I get about 2K when I run the above SELECT by itself, so I know there should be some results, I'm just messing up somewhere still.

Would I better off with a JOIN? How would that look?

Thanks!
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 41859411
Hi there, remaining issue is because your "exists" statement isn't actually looking at the first query.  For the not in, you would have to specify one column.

The not in should work if you changed to:

and ac.AccountID not in (select p.AccountID from ....
0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 41859415
I'd be inclined to join on the second query though rather than having another "in" query:

SELECT  ac.*
FROM [PCAR_Data].[dbo].[Account] ac 
WHERE ac.AccountStatusCode in ('AC', 'IN', 'PP') and ac.PracticeID = 194
and ac.AccountID not in (
	SELECT p.AccountID
	FROM [PCAR_Data].[dbo].[Payment] p
	join Account a on p.AccountID = a.AccountID
	where a.PracticeID = 194
	AND p.PaymentPostedDate > DATEADD(DAY, -30, CAST(GETDATE() AS date))
)

Open in new window

0
 

Author Closing Comment

by:brucegust
ID: 41862389
Thank you, friend!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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