Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

971 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