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 10:26 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:53 AM
Points: 1,176, Visits: 778
thanks for the question and the input from everyone today.
Post #1263803
Posted Thursday, March 8, 2012 10:49 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 10:56 AM
Points: 3,417, Visits: 2,035
Good question and discussion. I always enjoy order of precedence questions.
Post #1263825
Posted Friday, March 9, 2012 12:37 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, November 20, 2014 4:37 AM
Points: 305, Visits: 180
Thank you for the question.
I deduced the result using operator precedence and parsing the input by hand (or head?).
Typed it into my local, friendly SSMS, and got the same result.
Although I hadn't really thought about it, the "operator precedence" combined with the "test data might thwart you" nature of this question really made the discussion pay off.

Keep your databases clean.
Post #1264161
Posted Friday, March 9, 2012 4:07 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 29, 2013 3:07 AM
Points: 11, Visits: 126
Really a nice question, make me learn more about this basic knowledge.

However, in real case, we really seldom use "Union" and "Union all" together just like the questions. always use any one of it to make the script consistence

anyway, now I am much more clearer about these two commands and the sequence effect
Post #1264229
Posted Friday, March 9, 2012 4:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 7,924, Visits: 9,650
Nice question, and the explanation has provoked an interesting discussion.

Now there's maybe a problem with the explanation adopted as the result of that discussion: is it anywhere documented that UNION strings associate left to right unless overruled by parantheses, or could the optimiser decide (for whatever reason) to go right to left, or some arbitrary order? I guess this doesn't happen in existing versions (but haven't done extensive testing to verify that, it is purely a guess), but is that part of the T-SQL definition or something that a future quick fix could change? If it could change, then the right answer would presumably be "it depends what the optimiser chooses to do".


Tom
Post #1264246
Posted Friday, March 9, 2012 5:28 AM
SSC-Addicted

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

Group: General Forum Members
Last Login: Monday, November 17, 2014 7:32 AM
Points: 412, Visits: 1,412
L' Eomot Inversé (3/9/2012)
Nice question, and the explanation has provoked an interesting discussion.

Now there's maybe a problem with the explanation adopted as the result of that discussion: is it anywhere documented that UNION strings associate left to right unless overruled by parantheses, or could the optimiser decide (for whatever reason) to go right to left, or some arbitrary order? I guess this doesn't happen in existing versions (but haven't done extensive testing to verify that, it is purely a guess), but is that part of the T-SQL definition or something that a future quick fix could change? If it could change, then the right answer would presumably be "it depends what the optimiser chooses to do".


I have not yet found any official docu stating that it is always evaluated left-to-right. So, I think you are right: "it may depend". BUT, if in a future version of SQL server this changes, then I am going to ask MS to re-evaluate a lot of change requests and bug reports on Connect that have been rejected because "the change could break existing applications" . Waiting for the day!




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 #1264278
Posted Monday, March 12, 2012 7:56 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, June 13, 2014 3:03 PM
Points: 594, Visits: 655
Koen Verbeeck (3/8/2012)
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.


Well put. Great question!


Peter Trast
Microsoft Certified ...(insert many literal strings here)
Microsoft Design Architect with Alexander Open Systems
Post #1265201
Posted Wednesday, March 14, 2012 7:45 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 3:44 AM
Points: 292, Visits: 262
FYI. Doesn't work on SQL 2005

Error showing on line "Insert Into #t1(col) Values (1),(1);"

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.

I believe this function is not available until 2008!


Regards
ld

Stoke-on-Trent
United Kingdom

If at first you don't succeed, go to the pub and drink away your current thought plan.
Post #1266722
Posted Wednesday, March 14, 2012 8:20 AM
SSC-Addicted

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

Group: General Forum Members
Last Login: Monday, November 17, 2014 7:32 AM
Points: 412, Visits: 1,412
Yes, you're right. Forgot about that. It's just for setting up the sample data, the union will still behave the same given the same data. i.e. if you insert the data using insert() select-statements you'll still be able to run it and get the same results.

As I said, it was tested on SQL 2008R2 only.




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 #1266761
Posted Wednesday, March 14, 2012 7:53 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:45 PM
Points: 1,589, Visits: 253
Good question. Thanks for submitting.

http://brittcluff.blogspot.com/
Post #1267184
« Prev Topic | Next Topic »

Add to briefcase «««12345»»

Permissions Expand / Collapse