Solved

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

Posted on 2016-10-25
5
17 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
  • 3
  • 2
5 Comments
 
LVL 22

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 22

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 22

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

19 Experts available now in Live!

Get 1:1 Help Now