Possible Duplicate:
Results order with union query
I’m using SQL Server 2008.
I have 2 selects joined using UNION. The second select adds one row. I want this row always show on the bottom. How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can include another column in each part of the union query and use that to sort on:-
This will put the rows from the second select statement after the first one, within which they will be in order of col1.
Note that as you are sorting outside of the inner query the only reason for using union rather than union all is if you want to eliminate duplicates. The duplicates in this case must come from within either of the queries rather than being common to both, because the sortorder column makes them distinct between the two queries. So you probably will want to use union all.