Combining union and union all

  • thanks for the question and the input from everyone today.

  • Good question and discussion. I always enjoy order of precedence questions.

  • 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.

  • 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

  • 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

  • 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" :cool:. Waiting for the day! πŸ˜€



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


    If you don't have time to do it right, when will you have time to do it over?

  • 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

  • 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.

  • 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[/url]
    Posting Performance Based Questions - Gail Shaw[/url]
    Hidden RBAR - Jeff Moden[/url]
    Cross Tabs and Pivots - Jeff Moden[/url]
    Catch-all queries - Gail Shaw[/url]


    If you don't have time to do it right, when will you have time to do it over?

  • Good question. Thanks for submitting.

    http://brittcluff.blogspot.com/

  • got it right and learn a behaviour great love it πŸ™‚

  • Answer of the question is wrong !!

    select @@VERSION

    create table #t1 (col int not null)

    create table #t2 (col int not null)

    create table #t3 (col int not null)

    insert #t1 values (1), (1)

    insert #t2 values (2)

    insert #t3 values (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 all

    select COL from #t3

    Result:

    Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) Apr 22 2011 19:23:43 Copyright (c) Microsoft Corporation Data Center Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: )

    COL

    1

    2

    3

    COL

    1

    1

    2

    3

  • tibrize (7/20/2012)


    Answer of the question is wrong !!

    Nope, you typed it wrong. πŸ˜› The very last query is a UNION and you have UNION ALL

    select @@VERSION

    create table #t1 (col int not null)

    create table #t2 (col int not null)

    create table #t3 (col int not null)

    insert #t1 values (1), (1)

    insert #t2 values (2)

    insert #t3 values (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 all

    select COL from #t3

    _______________________________________________________________

    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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • not confused @ all

    What you don't know won't hurt you but what you know will make you plan to know better
  • great question, although I got it right, but I am glad i read the posts .Otherwise , I may have ended with wrong idea.

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

Viewing 15 posts - 31 through 45 (of 49 total)

You must be logged in to reply to this topic. Login to reply