dbTalk Databases Forums  

overlap in where clause produces intersection, not union

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss overlap in where clause produces intersection, not union in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
migurus
 
Posts: n/a

Default overlap in where clause produces intersection, not union - 03-03-2010 , 09:36 PM






I am not clear why SQL Server eliminates dups from my query below

declare @s table ( id int );
insert into @s (id)
select 1 union
select 2 union
select 3 union
select 4 union
select 5

(5 row(s) affected)

select count(id) from @s where id <= 3
-----------
3

select count(id) from @s where id >= 2
-----------
4

HERE is where I am puzzled: I thought "where id <= 3 OR id >= 2"
should produce overlapping results, i.e. rows with id=2 and id=3 OR'ed
together would be repeted in the resulting rows, but the results are
as if I specified DISTINCT

select id from @s where id <= 3 OR id >= 2
id
-----------
1
2
3
4
5

*** expecetd to see 1,2,2,3,3,4,5

select count(id) from @s where id <= 3 OR id >= 2
-----------
5

*** expected to see 7

Why it is so?

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: overlap in where clause produces intersection, not union - 03-03-2010 , 09:48 PM






The WHERE clause is used to specify predicates filtering the result set. It does not imply union operation. To
accomplish what you want you have to use UNION ALL:

SELECT id FROM @s WHERE id <= 3
UNION ALL
SELECT id FROM @s WHERE id >= 2;

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.