Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to NOT remove all occurances

Status
Not open for further replies.

sqller

Programmer
Sep 19, 2011
1
IN
I am a newbie. Sorry if the question is stupid :p. I have a table as follows

A B C D
x 1
y 1
t z 1

my query is as follows:

Select D from table where A = 'x' OR B = 'y' OR C = 'n' EXCEPT Select D from table where (A NOT EQUALS 'x' AND A NOT EQUALS '')
OR (B NOT EQUALS 'y' AND B NOT EQUALS '')
OR (C NOT EQUALS 'n' AND C NOT EQUALS '')
The first query gives me 1,1 and the second gives me 1. Now I want the result to be 1,1 EXCEPT 1 i.e 1. However, SQL server removes all occurrences of 1 from the first result(which does make sense, but that's how my logic is right now).

Any kind of help would be highly appreciated.
 
Select D
from test
where A = 'x'
OR B = 'y'
OR C = 'n'

returns
D
1
1

Select D
from test
where (A <> 'x' AND A <> '')
OR (B <> 'y' AND B <> '')
OR (C <> 'n' AND C <> '')

Returns
D
1

EXCEPT returns any distinct values from the left query that are not also found on the right query.

Meaning that nothing would be returned.

Perhaps you could show us the result you wanted and we could help with that.

Simi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top