Solved

How to get a union query to return distinct rows

Posted on 2011-02-15
3
616 Views
Last Modified: 2012-06-27
Hi

Is there a way to get a union query to only return a distinct set of rows if there is overlap in the rows returned by the multiple select statements?

thanks
0
Comment
Question by:andieje
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34900286
UNION does already a distinct, implicitly (unlike UNION ALL which does not perform a distinct).

however, UNION and DISTINCT both do the distinct on the returned column sets, not just on a "primary key" field.

so, you need to clarify what exactly you consider "distinct", and apply some sql techniques accordingly.
consider reading this article to find explanations and solution: http://www.experts-exchange.com/A_3203.html
0
 

Author Closing Comment

by:andieje
ID: 34900469
thanks
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900487
check these

(select 1 union select 2)
1
2

(select 1 union select 3 union select 1 union select 2)
1
2
3

(select 1 union select 2)
union
(select 1 union select 3 union select 1 union select 2)
1
2
3
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now