SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query for 'never in'?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Bazinga
Starting Member

19 Posts

Posted - 10/01/2013 :  11:33:24  Show Profile  Reply with Quote
I would like to do something like this:

SELECT DISTINCT t.ID
FROM table t
WHERE t.code IS NEVER IN 'ABC'

So if the person with ID #000123 has 50 records each with a different code, I only want their ID to appear IF they DONT have a record with code ABC. Not sure how???

bitsmed
Constraint Violating Yak Guru

319 Posts

Posted - 10/01/2013 :  11:46:04  Show Profile  Reply with Quote
Try this:

select id
  from table
 group by id
 having sum(case when code='ABC' then 1 else 0 end)=0
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22742 Posts

Posted - 10/02/2013 :  03:43:54  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
HAVING clause can be effectively used in above case. For more information refer this http://beyondrelational.com/modules/2/blogs/70/posts/19573/efficient-filtering-with-having-clause.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000