?
Solved

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

Posted on 2016-10-25
5
Medium Priority
?
33 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 2000 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

777 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