Solved

SQL where OR clause

Posted on 2014-03-29
5
306 Views
Last Modified: 2014-04-07
Hi
I have the following results

Name  Status date
-------   ------- ------
mark    1        29/03/2014
mark    0        09/03/2014
mark    1        19/03/2014
John     0        19/03/2014
bob      1        19/03/2014
 
I have a the SQL
select SSNumber  from tb2
where SSNumber NOT IN (select SSNumber from tbl where status =1)

But as MARK has status 0 and 1 it is showing in the list
Really i want this

if MARK has only a status 1 then it should NOT show
If MARK has Status 1 and 0 then it should NOT show
If MARK only has status 0 then it should show

What SQL would i need to do this
I guess it would be something like
select SSNumber from tbl where status =1 (or occurrence of status = 0 and another with status = 1)

 

hope that makes sense
this could be super simple but really tired and my brain hurts
0
Comment
Question by:websss
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39963670
please try this:
select SSNumber  
from tbl t
where NOT EXISTS(select NULL from tbl t2 where t2.status =1 and t2.SSNumber  = t1.SSNumber ) 

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
ID: 39963754
Your query just needs a slight modification -
select SSNumber  from tb2
WHERE status = 0 AND
SSNumber NOT IN (select SSNumber from tbl where status =1)
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39964961
Assuming SSNumber is unique for a person (i.e., it's not primary-key):
select	SSNumber
from	tb2
group
by	SSNumber
having	max(status) = 0

Open in new window

0
 

Author Comment

by:websss
ID: 39965010
Correct social security number is unique
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39965888
how can that value of SSN be unique (in that table) if you have several rows for the same person? I don't think that that field is the primary key in that table?
still, if you only need the SSN value, John's suggestion is perfectly valid.

other than that: does my suggestion work? if not, what are the example where it does not work?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now