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
Solved

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

Posted on 2016-10-25
5
25 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

790 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