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

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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:

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

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

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

from t_account a
group by a.account
having MAX(case when a.shs in ('525','528') then 1 else 0 end) = 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 );

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' ))
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

But even in Oracle Scott's version should work.
DCUnitedAuthor Commented:
It worked perfectly. I had an error on my end. Didn't see it at first.
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.