MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 12th, 2004, 09:04 AM
DanielSchaffer DanielSchaffer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 1 DanielSchaffer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I thought UNION didn't select duplicate records?

I'm using this huge SQL statement:

strSQL = "(SELECT Tasks.*,TasksCompleted.*" _
& " FROM TaskListItems" _
& " INNER JOIN (Tasks " _
& " LEFT JOIN TasksCompleted ON Tasks.ID = TasksCompleted.TaskID)" _
& " ON TaskListItems.TaskID = Tasks.ID" _
& " WHERE (TaskListItems.TaskListID=" & TaskListID _
& " AND TasksCompleted.UserID=" & CurUser.ID _
& " AND TaskListItems.Removed=False" _
& " AND Tasks.AFSCID=" & CurUser.AFSC.ID _
& " AND Tasks.ParentID=" & ID & "))" _
& " UNION" _
& " (SELECT Tasks_1.*,TasksCompleted_1.* FROM Tasks AS Tasks_1 LEFT JOIN TasksCompleted AS TasksCompleted_1 ON Tasks_1.ID = TasksCompleted_1.TaskID" _
& " WHERE Tasks_1.Heading=True" _
& " AND Tasks_1.AFSCID=" & CurUser.AFSC.ID _
& " AND Tasks_1.ParentID=" & ID & ")" _
& " UNION" _
& "(SELECT Tasks_2.*,TasksCompleted_2.*" _
& " FROM TaskListItems AS TaskListItems_2" _
& " INNER JOIN (Tasks AS Tasks_2 " _
& " LEFT JOIN TasksCompleted AS TasksCompleted_2 ON Tasks_2.ID = TasksCompleted_2.TaskID)" _
& " ON TaskListItems_2.TaskID = Tasks_2.ID" _
& " WHERE (TaskListItems_2.TaskListID=" & TaskListID _
& " AND (TasksCompleted_2.UserID<>" & CurUser.ID _
& " OR TasksCompleted_2.UserID Is Null)" _
& " AND TaskListItems_2.Removed=False" _
& " AND Tasks_2.AFSCID=" & CurUser.AFSC.ID _
& " AND Tasks_2.ParentID=" & ID & "))"

But in some cases, it returns duplicate records from the "Tasks" table. I thought UNION only selects distinct records, while UNION ALL would create duplicates?

Reply With Quote
  #2  
Old February 12th, 2004, 10:11 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
a UNION query does not eliminate "duplicates" from one of the queries involved in a join

a UNION query eliminates duplicate result set rows

this depends entirely on the values in the columns of the SELECT list, nothing else

by the time the UNION sort takes place -- you realize it is sorting your entire result set, right? -- it no longer matters which table a particular result set column came from

do yourself a favour, run your queries separately, and save the output of each one separately

if your final result set appears to have duplicate rows, take another look, there is bound to be something different in one of the columns
__________________
r937.com | rudy.ca

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > I thought UNION didn't select duplicate records?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway