Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL where OR clause

Posted on 2014-03-29
5
Medium Priority
?
316 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 32

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 143

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

876 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