Return accounts not in criteria but accounts could have multiple entries

I need to return a list of accounts that are not in specific SHS codes. These accounts could me listed multiple times and could include both SHS codes that need to be excluded & include.

Account     |   SHS
1234               525
1234               600
2345               600
3456               525
3456               528
4567               489

Select
a.account
from t_account a
where a.shs not in ('525','528')

Return accounts not in SHS 525 or 528
Query would return only accounts # 2345 &4567
DCUnitedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
E.g.

DECLARE @Sample TABLE ( Account INT, SHS INT );

INSERT INTO @Sample
VALUES	( 1234, 525	),
	( 1234, 600	),
	( 2345, 600	),
	( 3456, 525	),
	( 3456, 528	),
	( 4567, 489 );

SELECT	a.account
FROM	@Sample a
WHERE	NOT a.account IN 
(
	SELECT	i.account 
	FROM	@Sample i 
	WHERE	i.shs IN ( '525','528' )
);

Open in new window

0
Scott PletcherSenior DBACommented:
You can do that much more efficiently, particularly if you want to list only the account:

Select
a.account
from t_account a
group by a.account
having MAX(case when a.shs in ('525','528') then 1 else 0 end) = 0
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank GairolaSr. Application Support Engg.Commented:
This should run fine.

drop table #temp
create TABLE #temp ( Account INT, SHS INT );

INSERT INTO #temp
VALUES      ( 1234, 525      ),
      ( 1234, 600      ),
      ( 2345, 600      ),
      ( 3456, 525      ),
      ( 3456, 528      ),
      ( 4567, 489 );

SELECT      a.account
FROM      #temp a
WHERE      a.shs not IN ( '525','528' )
and not exists
(SELECT      top 1 1
      FROM      #temp i
      WHERE      a.account=i.account and i.shs IN ( '525','528' ))
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DCUnitedAuthor Commented:
I forgot to mention it's in oracle
0
ste5anSenior DeveloperCommented:
Wrong tags then.

But even in Oracle Scott's version should work.
0
DCUnitedAuthor Commented:
It worked perfectly. I had an error on my end. Didn't see it at first.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.