Solved

How to get a union query to return distinct rows

Posted on 2011-02-15
3
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

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 56

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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