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)
 select where 2 conditions

Author  Topic 

mrki_81
Starting Member

4 Posts

Posted - 2012-04-19 : 12:32:13
Hi,

I have a query where I want all data except when name = john or michael and if their status is active

So the query looks like
select * from database
where (name not in('John','Michael') and status <> 'active')


and here is the problem:
it filters first all 'John' and 'Michael' names out and then it filters ALL data with status 'active' out. but this is not correct. There is a 'Frank' with status 'active' and he should be displayed.

Any suggestions?
Thank you

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-04-19 : 12:39:45



Try this
select * from database
where (name ='John' or name ='Michael') and status <> 'active')
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-04-19 : 12:42:51
Ignore First one
This should work

Try this
select * from database
where (name ='John' or name ='Michael') OR status <> 'active'
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-19 : 12:55:28
For me, using the "NOT" outside of the conditions is easier to read. For example, one of these:
-- All except John and Michael in non-active states
WHERE NOT (name in('John','Michael') and coalesce(status,'') <> 'active')

-- All except John and Michael when they are active.
WHERE NOT (name in('John','Michael') and status = 'active')
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-04-19 : 12:56:00
I'm not clear on what you want for output. Given this sample data, what should the query return?
DECLARE @T TABLE (Name VARCHAR(30), Status VARCHAR(20))

INSERT @T
VALUES
('Frank', 'Active'),
('Frank', 'Not Active'),
('John', 'Active'),
('John', 'Not Active'),
('Michael', 'Active'),
('Michael', 'Not Active')
It'll help if you supply sample data and expected output in the future.
Go to Top of Page

mrki_81
Starting Member

4 Posts

Posted - 2012-04-19 : 16:08:32
thank you all, will try your code tomorrow

@lamprey

output should be:
Frank, Aktive
Frank, Not Active
John, Not Active
Michael, Not Active
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-04-19 : 16:14:26
DECLARE @T TABLE (Name VARCHAR(30), Status VARCHAR(20))
INSERT @T

select 'Frank', 'Active' union all
select 'Frank', 'Not Active' union all
select 'John', 'Active' union all
select 'John', 'Not Active' union all
select 'Michael', 'Active' union all
select 'Michael', 'Not Active'

Select * from @t
where
(name in ('Michael','John') and Status = 'Not Active')
or
(not name in ('Michael','John') )
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-04-19 : 17:09:43
Sunitabecks code is probably the most succinct:
WHERE NOT (name in('John','Michael') and status = 'active')
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-04-20 : 12:38:04
duh... Long day:) yeah don't use the query I provided, it will give correct results but more overhead than is needed!


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-04-20 : 13:57:22
;)
Go to Top of Page

mrki_81
Starting Member

4 Posts

Posted - 2012-04-23 : 03:39:19
Thank you all for you help! You are a really nice community.
@Lamprey
Your code works perfect.

Thanks!
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2012-04-23 : 05:26:31
How about Except ?


select * from @T
except
select * from @T where Name in('John','Michael') and Status='Active'


After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-23 : 11:43:09
quote:
Originally posted by Sachin.Nand

How about Except ?


select * from @T
except
select * from @T where Name in('John','Michael') and Status='Active'


After Monday and Tuesday even the calendar says W T F ....

While logically correct, this would result in higher query cost due to the need for two passes against the table followed by the merge join or nested loops or whatever else query optimizer may choose to filter the data.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2012-04-23 : 12:02:43
quote:
Originally posted by sunitabeck

quote:
Originally posted by Sachin.Nand

How about Except ?


select * from @T
except
select * from @T where Name in('John','Michael') and Status='Active'


After Monday and Tuesday even the calendar says W T F ....

While logically correct, this would result in higher query cost due to the need for two passes against the table followed by the merge join or nested loops or whatever else query optimizer may choose to filter the data.



Was performance an issue here ?

The reason I showed the example with except was because the OP had actually bolded the word except in his original post.

quote:
I have a query where I want all data except when name = john or michael and if their status is active


So it was kind of suggestion that except can be used in sql.

After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-23 : 12:13:34
quote:

Was performance an issue here ?
Writing code with performance in mind only when performance becomes an issue is not a sound practice, in my humble opinion.

quote:
The reason I showed the example with except was because the OP had actually bolded the word except in his original post.
Hence the reason for my post as well. While your suggestion sounded syntactically very much like what the OP was asking for, I was pointing out that that would not be a good choice.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2012-04-23 : 12:48:24
If you want to talk about performance then lets see..you suggested NOT IN.Well for your kind info it is one of the most pathetically worst performing operators.So your humble opinion of you suggesting an "optimized query" to the OP does not stand. Need a proof ???

Check this out..

use AdventureWorks
go

set statistics io on
go
select distinct CarrierTrackingNumber from Sales.SalesOrderDetail where CarrierTrackingNumber not in ('2E63-4616-B1') and CarrierTrackingNumber IS not null
go

select CarrierTrackingNumber from Sales.SalesOrderDetail where CarrierTrackingNumber IS not null
except
select '2E63-4616-B1'


Logical reads with the NOT IN is 327 and the one with Except is 323.

So accordingly keeping your suggestion in mind of "Writing code with performance in mind only when performance becomes an issue is not a sound practice" Except performs better than NOT IN.



After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-23 : 13:00:51
quote:
Originally posted by Sachin.Nand

If you want to talk about performance then lets see..you suggested NOT IN.Well for your kind info it is one of the most pathetically worst performing operators.So your humble opinion of you suggesting an "optimized query" to the OP does not stand. Need a proof ???

Check this out..

use AdventureWorks
go

set statistics io on
go
select distinct CarrierTrackingNumber from Sales.SalesOrderDetail where CarrierTrackingNumber not in ('2E63-4616-B1') and CarrierTrackingNumber IS not null
go

select CarrierTrackingNumber from Sales.SalesOrderDetail where CarrierTrackingNumber IS not null
except
select '2E63-4616-B1'


Logical reads with the NOT IN is 327 and the one with Except is 323.

So accordingly keeping your suggestion in mind of "Writing code with performance in mind only when performance becomes an issue is not a sound practice" Except performs better than NOT IN.



After Monday and Tuesday even the calendar says W T F ....

The example you quote is not a fair comparison. The select that comes after the EXCEPT clause in your example will generate a trivial plan. So that is not representative of a real query that uses the EXCEPT construct.

If you like to do a real comparison: Vinnie posted DDL for test data on 04/19/2012 : 16:14:26. Copy that, write the except query and the one that Vinnie is proposing in that posting and compare the relative costs.

That said, from what I can tell, OP has accomplished what s/he wanted to accomplish and is long gone. So I don't think either of us is going to be able to convince the OP to use one or the other.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2012-04-23 : 13:28:48
The point here is not on suggesting the OP on which method is better and which one is not but more of rather refraining from making one liner suggestions "of performance tips" without any substantial proof.

Also are you really serious in suggesting me to test performance on a table having 6 records ?

Once again I would reiterate that performance of the queries vary in different scenarios.So my initial posting on the thread was to make OP aware of the EXCEPT operator and nothing more than that.

After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-23 : 13:38:17
quote:
Originally posted by Sachin.Nand

The point here is not on suggesting the OP on which method is better and which one is not but more of rather refraining from making one liner suggestions "of performance tips" without any substantial proof.

Also are you really serious in suggesting me to test performance on a table having 6 records ?

Once again I would reiterate that performance of the queries vary in different scenarios.So my initial posting on the thread was to make OP aware of the EXCEPT operator and nothing more than that.

After Monday and Tuesday even the calendar says W T F ....

I would look at the query plan for the table with six records. If it seems to you that query plan won't scale, it is easy enough to replicate the data as many times as required to convince yourself which query would perform better. The "performance tip" that I offered is backed by fact, which you can verify for yourself in this manner.

I am in complete agreement that performance will vary in different scenarios. All I am saying is that in this specific scenario, the query you proposed would perform poorly compared to what others had proposed.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2012-04-24 : 02:37:55
Are you telling me to check the performance on a table that has a design which is no where near to what a RDBMS is suppossed to be.I am surely not gonna do that.

After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-24 : 07:15:15
quote:
Originally posted by Sachin.Nand

Are you telling me to check the performance on a table that has a design which is no where near to what a RDBMS is suppossed to be.I am surely not gonna do that.

After Monday and Tuesday even the calendar says W T F ....

I was going to do the performance test and post the results for you. But while reading through the thread to refresh my memory, I realized to my dismay, that your posts sound like I may have really offended you. If I did, I am truly sorry - that was not my intention at all. If it was the tone of my posts that perhaps seemed rather curt that caused it, it was not meant to be personally offensive.

I was not even focusing on the text in your posts; I was focused on the queries, or else I would not have missed the emotions expressed in your responses such as these:

quote:
If you want to talk about performance then lets see..you suggested NOT IN.Well for your kind info it is one of the most pathetically worst performing operators.So your humble opinion of you suggesting an "optimized query" to the OP does not stand. Need a proof ???


I hang around on this forum mostly to learn from the abundance of friendly expertise present here and also to help others when I am able to. Offending you - or anyone else - is the LAST thing I want to/would do.

Again, I am sorry and apologize for anything I said that came across as offensive. Technically, I have nothing more to add than what I have already said.

Sunita.

Edit: For anyone uninitiated in the vaguaries of Indian English: The expression "for your kind information", which, on the face of it sounds magnanimous and generous, is neither meant to be kind nor informative. To the contrary, it is often used to express the speaker's lack of respect for the other person's cognitive or analytical skills and is often spoken in a tone of controlled anger that allows the speaker to spew saracasm and vitriol, while giving the appearance that s/he is being calm and rational.

An elite version of the Forrest Gump like expression of "You stupid or what!", if you will, albeit, just as vicious.
Go to Top of Page
    Next Page

- Advertisement -