SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


using multiple UNION in query


using multiple UNION in query

Author
Message
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18995 Visits: 20461
Thanks Sean - that was the point that I was hoping to make.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14524 Visits: 15980
Sean Lange (5/16/2013)
John Mitchell-245523 (5/16/2013)
Phil Parkin (5/16/2013)
lnardozi 61862 (5/16/2013)
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.


Why not?

I suppose because UNION eliminates duplicates you only need to do it once - as long as you make sure that UNION is the last one evaluated. I don't know whether the query optimizer is smart enough to do that implicitly if you use more than one UNION - so it may or may not make a difference from a performance point of view.

John


But that is only good if you want to eliminate ALL duplicates. It is very possible that you have 3 tables and in the first 2 tables you want duplicates but if the same value shows up in the third table you want only distinct values. Other times maybe you want to include all duplicates.

The blanket statement that "you never use more than one UNION, which is the last one. All the rest should be UNION ALL." is just completely not true. There are time when that is correct but not always.

I'm not sure I understand. Those duplicates from the first two tables will be eliminated by the UNION operator. Try this:
SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION ALL
SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION
SELECT * FROM (VALUES (4)) z(C)



John
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5112 Visits: 5478
John Mitchell-245523 (5/16/2013)
Sean Lange (5/16/2013)
John Mitchell-245523 (5/16/2013)
Phil Parkin (5/16/2013)
lnardozi 61862 (5/16/2013)
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.


Why not?

I suppose because UNION eliminates duplicates you only need to do it once - as long as you make sure that UNION is the last one evaluated. I don't know whether the query optimizer is smart enough to do that implicitly if you use more than one UNION - so it may or may not make a difference from a performance point of view.

John


But that is only good if you want to eliminate ALL duplicates. It is very possible that you have 3 tables and in the first 2 tables you want duplicates but if the same value shows up in the third table you want only distinct values. Other times maybe you want to include all duplicates.

The blanket statement that "you never use more than one UNION, which is the last one. All the rest should be UNION ALL." is just completely not true. There are time when that is correct but not always.

I'm not sure I understand. Those duplicates from the first two tables will be eliminated by the UNION operator. Try this:
SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION ALL
SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION
SELECT * FROM (VALUES (4)) z(C)



John


try this:

SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION
SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION all
SELECT * FROM (VALUES (4),(3)) z(C)



However, until this sort of functionality is really required, I wouldn't mix both UNION and UNION ALL in one query.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26540 Visits: 17557
John Mitchell-245523 (5/16/2013)
Sean Lange (5/16/2013)
John Mitchell-245523 (5/16/2013)
Phil Parkin (5/16/2013)
lnardozi 61862 (5/16/2013)
Just for completeness sake, you never use more than one UNION, which is the last one. All the rest should be UNION ALL.


Why not?

I suppose because UNION eliminates duplicates you only need to do it once - as long as you make sure that UNION is the last one evaluated. I don't know whether the query optimizer is smart enough to do that implicitly if you use more than one UNION - so it may or may not make a difference from a performance point of view.

John


But that is only good if you want to eliminate ALL duplicates. It is very possible that you have 3 tables and in the first 2 tables you want duplicates but if the same value shows up in the third table you want only distinct values. Other times maybe you want to include all duplicates.

The blanket statement that "you never use more than one UNION, which is the last one. All the rest should be UNION ALL." is just completely not true. There are time when that is correct but not always.

I'm not sure I understand. Those duplicates from the first two tables will be eliminated by the UNION operator. Try this:
SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION ALL
SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION
SELECT * FROM (VALUES (4)) z(C)



John


The difference is that you don't have any values in z that are also in either of the first two tables.

Let's say you wanted distinct values from x and y but you wanted to include duplicates from z if they exist. In this scenario you have to change the order of the UNION and UNION ALL.


SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION
SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION ALL
SELECT * FROM (VALUES (4), (1)) z(C)



--EDIT--

Didn't see Eugene's post before I hit reply. I agree that you should not mix them unless it is really necessary. It does however prove that the blanket statement posted above is just not true. As with nearly everything in SQL "it depends". :-P

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14524 Visits: 15980
But that's because you changed the order. The proviso was that the UNION operator is the last one to be evaluated. If you use parentheses to make it so, you get the same results as before:

SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION
(SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION all
SELECT * FROM (VALUES (4),(3)) z(C))



John

Edit - I think I understand what you're saying. I need to play around and see what happens when you have more than one UNION. I'll see if I get time to do that tomorrow.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26540 Visits: 17557
John Mitchell-245523 (5/16/2013)
But that's because you changed the order. The proviso was that the UNION operator is the last one to be evaluated. If you use parentheses to make it so, you get the same results as before:

SELECT * FROM (VALUES (1), (1), (2)) x(A)
UNION
(SELECT * FROM (VALUES (2), (3), (3)) y(B)
UNION all
SELECT * FROM (VALUES (4),(3)) z(C))



John

Edit - I think I understand what you're saying. I need to play around and see what happens when you have more than one UNION. I'll see if I get time to do that tomorrow.


Right but they made a blanket statement that you should NEVER us more than one union and it should always be the last one. That is just simply false. It depends on the requirements of the result set.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14524 Visits: 15980
Yes, I agree that it was worded too strongly. Can you think of any cases where it isn't possible to rewrite a series of UNIONs so that it contains only one UNION, with the rest being converted to UNION ALLs?

John
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search