﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / How to use"Union ALL" to  join sql queries each containing order by clause / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 21:10:23 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>I just needed to show that your statement[quote]u have to ensure that the aliases of everything your selecting [b]must [/b]match between the top select and bottom select[/quote]is just wrong. Nothing more. But nothing less either.</description><pubDate>Thu, 04 Mar 2010 16:04:01 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>That's not correct. When using UNION or UNION ALL SQL server will use the alias of the first statement for all. Also, it will use the data type with the highest precedence for all queries.========================================================================A few examples:-- will failSELECT 1 AS aUNION SELECT 2 AS bORDER BY B--if both aliases were the same this wouldnt fail also what advantage is there to having two different aliases because the "b" alias gets lost in the mix. It becomes "a".-- will runSELECT 1 AS aUNION SELECT 2 AS b--no order is being applied here. if a=2 and b=1 it would return 2,1--to your point i suppose there is no reason at all to even alias any but the first select. interesting.</description><pubDate>Thu, 04 Mar 2010 15:30:59 GMT</pubDate><dc:creator>BaldingLoopMan</dc:creator></item><item><title>RE: How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>[quote][b]BaldingLoopMan (3/4/2010)[/b][hr]also u have to ensure that the aliases of everything your selecting must match between the top select and bottom select. these aliases is what you will put in your order by[/quote]That's not correct. When using UNION or UNION ALL SQL server will use the alias of the first statement for all. Also, it will use the data type with the highest precedence for all queries.A few examples:[code="sql"]-- will failSELECT '1a'AS aUNION SELECT 2 AS b-- will failSELECT 1 AS aUNION SELECT 2 AS bORDER BY B-- will runSELECT 1 AS aUNION SELECT 2 AS b[/code]</description><pubDate>Thu, 04 Mar 2010 15:10:04 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>also u have to ensure that the aliases of everything your selecting must match between the top select and bottom select. these aliases is what you will put in your order by</description><pubDate>Thu, 04 Mar 2010 14:55:44 GMT</pubDate><dc:creator>BaldingLoopMan</dc:creator></item><item><title>RE: How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>If you still want order by in both querries use inner querry, but ideally using order by at the end of the both querries should also work for you</description><pubDate>Thu, 04 Mar 2010 04:12:37 GMT</pubDate><dc:creator>Giri Duddu</dc:creator></item><item><title>RE: How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>Remove the Order By Clause from the first statement and put it only in the last statement</description><pubDate>Wed, 03 Mar 2010 21:28:06 GMT</pubDate><dc:creator>Kingston Dhasian</dc:creator></item><item><title>How to use"Union ALL" to  join sql queries each containing order by clause</title><link>http://www.sqlservercentral.com/Forums/Topic876492-392-1.aspx</link><description>I have 2 sql statements  and each sql statement is having orderby clause when i am use" UNION ALL " to join them i am getting an error.</description><pubDate>Wed, 03 Mar 2010 20:16:10 GMT</pubDate><dc:creator>Ismail Ahmed</dc:creator></item></channel></rss>