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
 General SQL Server Forums
 New to SQL Server Programming
 Subquery vs. expression

Author  Topic 

dmilam
Posting Yak Master

185 Posts

Posted - 2010-06-08 : 12:30:57
I can't figure out why these two counting queries should give me different results:


declare @Tmp table
(String varchar(50))
insert into @Tmp (String)
select 'a10'
union all
select 'a09'
union all
select 'a08'

select count(distinct(c.id)) as 'select * from @Tmp'
from Db1.dbo.Table1 as c
where c.name in (select * from @Tmp) --use subquery
and c.orderstatusname in ('Shipped','Taken','Back-Ordered')
and c.OrderType = 'Regular'

select count(distinct(c.id)) as 'a08,a09,a10'
from Db1.dbo.Table1 as c
where c.name in ('a08','a09','a10') --use expression [OR]
and c.orderstatusname in ('Shipped','Taken','Back-Ordered')
and c.OrderType = 'Regular'


I get a higher number from the first query.

dmilam
Posting Yak Master

185 Posts

Posted - 2010-06-08 : 12:37:16
Nevermind. There was a typo in my original query, not reproduced in the edited version I have used here.
Go to Top of Page
   

- Advertisement -