Solved

SQL where OR clause

Posted on 2014-03-29
5
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

623 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