Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Combining union and union all Expand / Collapse
Author
Message
Posted Thursday, March 8, 2012 2:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:15 AM
Points: 13,017, Visits: 10,800
UNION queries are evaluated from left to right. If the last query contains duplicates and is preceded with UNION ALL, you will have duplicates in your result set.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1263478
Posted Thursday, March 8, 2012 2:23 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 AM
Points: 1,786, Visits: 490
Thanks for the question - and once again great discussion afterwards - learnt from that!
Post #1263480
Posted Thursday, March 8, 2012 2:23 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:49 AM
Points: 411, Visits: 1,403
tim.kay (3/8/2012)
Got it right and thought that I understood - now looking at the other posts I am slightly confused.

So am I, as I was very sure I had tested both situations: union followed by union all and union all followed by union... I see what happens, but it contradicts with my previous results. So I need to find out what I did wrong previously.




Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
Post #1263481
Posted Thursday, March 8, 2012 2:39 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:33 AM
Points: 2,542, Visits: 2,410
R.P.Rozema (3/8/2012)
tim.kay (3/8/2012)
Got it right and thought that I understood - now looking at the other posts I am slightly confused.

So am I, as I was very sure I had tested both situations: union followed by union all and union all followed by union... I see what happens, but it contradicts with my previous results. So I need to find out what I did wrong previously.

Your test is wrong because of values used:
1, 2 and 3 are different and they are not affected by the DISTINCT of UNION.
You should use same values!

Post #1263484
Posted Thursday, March 8, 2012 2:52 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:49 AM
Points: 411, Visits: 1,403
To finalize this: My explanation is wrong and my question + answer was correct only by luck.

The proper explanation has been given in this thread. To be sure I am getting it now I'll try to summarize it:

Union queries are interpreted left to right. If "union all" is followed by "union", the "union all" will return duplicates, but these will be filtered by the following "union". Other way around, if "union" is followed by "union all", any duplicates from the first 2 statements are filtered, but new duplicates may be introduced by the following "union all".

Parenthesis can be used to override the left-to-right evaluation.


An illustration can be given by putting more rows in the test tables:
create table #t1 (col int not null);
create table #t2 (col int not null);
create table #t3 (col int not null);

insert #t1 (col) values(1), (1);
insert #t2 (col) values(2), (2);
insert #t3 (col) values(3), (3);

select col from #t1
UNION
select col from #t2
UNION ALL
select col from #t3;

select col from #t1
UNION ALL
select col from #t2
UNION
select col from #t3;

And now the results are:
col
-----------
1
2
3
3

(4 row(s) affected)

col
-----------
1
2
3

(3 row(s) affected)

Seems like I was the first to learn something from my own question :).

Thanks for all the feedback!




Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
Post #1263488
Posted Thursday, March 8, 2012 2:56 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:33 AM
Points: 2,542, Visits: 2,410
R.P.Rozema (3/8/2012)
To finalize this: My explanation is wrong and my question + answer was correct only by luck.

The proper explanation has been given in this thread. To be sure I am getting it now I'll try to summarize it:

Union queries are interpreted left to right. If "union all" is followed by "union", the "union all" will return duplicates, but these will be filtered by the following "union". Other way around, if "union" is followed by "union all", any duplicates from the first 2 statements are filtered, but new duplicates may be introduced by the following "union all".

Parenthesis can be used to override the left-to-right evaluation.


An illustration can be given by putting more rows in the test tables:
create table #t1 (col int not null);
create table #t2 (col int not null);
create table #t3 (col int not null);

insert #t1 (col) values(1), (1);
insert #t2 (col) values(2), (2);
insert #t3 (col) values(3), (3);

select col from #t1
UNION
select col from #t2
UNION ALL
select col from #t3;

select col from #t1
UNION ALL
select col from #t2
UNION
select col from #t3;

And now the results are:
col
-----------
1
2
3
3

(4 row(s) affected)

col
-----------
1
2
3

(3 row(s) affected)

Seems like I was the first to learn something from my own question :).

Thanks for all the feedback!


OK!
Post #1263491
Posted Thursday, March 8, 2012 4:26 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 6:27 AM
Points: 1,258, Visits: 13,555
good question!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
Post #1263527
Posted Thursday, March 8, 2012 5:11 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 10:46 AM
Points: 2,734, Visits: 943
R.P.Rozema (3/8/2012)
To finalize this: My explanation is wrong and my question + answer was correct only by luck.
...


And why is it a great question?
Why UNION (ALL) is basic but very confusing and anyone can be tricked by it.
Also it remember us to be meticulous with precedence.

Thanks Rozema.
Post #1263551
Posted Thursday, March 8, 2012 5:28 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:17 PM
Points: 1,837, Visits: 2,180
Excellent question. Glad you could learn something too! Thanks for your efforts.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
Post #1263562
Posted Thursday, March 8, 2012 6:16 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 11:41 AM
Points: 973, Visits: 2,213
Nice.

(from next time rather converting to image, it will be better to paste the exact SQL)


ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
Post #1263570
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse