Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Multiple Where Condition - fields combination

Author  Topic 

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2013-10-02 : 09:31:20
Good day, i need help and i think its a real stupid question

i want a mutiple where clause:
field1,field2
25,0
25,22
2,0

SQL: where (field2 <> 0 and field1 = 25)
I want to return:
field1,field2
25,22
2,0

but it only returns: 25,22
i only want exclude the combination of (field2 <> 0 and field1 = 25)
but return everything else, please assist

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-02 : 09:40:44
quote:
Originally posted by ismailc

Good day, i need help and i think its a real stupid question

i want a mutiple where clause:
field1,field2
25,0
25,22
2,0

SQL: where (field2 <> 0 and field1 = 25)
I want to return:
field1,field2
25,22
2,0

but it only returns: 25,22
i only want exclude the combination of (field2 <> 0 and field1 = 25)
but return everything else, please assist



Not quite clear to me the rule that you want to use - can you give this a try?
;with cte as
( select *, row_number() over (partition by field1 order by case when field2 = 0 then 0 else 1 end) as RN
from YourTable
)
select field1,field2 from cte where RN = 1;
Go to Top of Page

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2013-10-02 : 09:44:15
Thank You - looks very complicated
should be simpler method
i want to exclude the combination of field1:25 & field2:0 = (250)
but return everything else
Go to Top of Page

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2013-10-02 : 09:49:04
Apologies - i was being silly

where ((field2 <> 0 and field 1= 25)
or (field2= 0and field1 <> 25)
or (field2<> 0 and field1 <> 25))

Thank You ALL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-04 : 03:14:20
quote:
Originally posted by ismailc

Apologies - i was being silly

where ((field2 <> 0 and field 1= 25)
or (field2= 0and field1 <> 25)
or (field2<> 0 and field1 <> 25))

Thank You ALL


you just need this

WHERE CASE WHEN field1 = 25 AND field2 = 0 THEN 0 ELSE 1 END = 1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -