Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

How to get a union query to return distinct rows

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
andieje
Asked:
andieje
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
andiejeAuthor Commented:
thanks
0
 
HainKurtSr. System AnalystCommented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now