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

How to use"Union ALL" to join sql queries each containing order by clause Expand / Collapse
Author
Message
Posted Wednesday, March 03, 2010 8:16 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, March 12, 2010 11:05 PM
Points: 14, Visits: 33
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.
Post #876492
Posted Wednesday, March 03, 2010 9:28 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 2,231, Visits: 4,202
Remove the Order By Clause from the first statement and put it only in the last statement


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #876521
Posted Thursday, March 04, 2010 4:12 AM
SSC-Addicted

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

Group: General Forum Members
Last Login: Monday, February 04, 2013 7:02 AM
Points: 459, Visits: 94
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
Post #876695
Posted Thursday, March 04, 2010 2:55 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:23 AM
Points: 304, Visits: 457
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
Post #877205
Posted Thursday, March 04, 2010 3:10 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731, Visits: 12,131
BaldingLoopMan (3/4/2010)
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

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 fail
SELECT '1a'AS a
UNION
SELECT 2 AS b

-- will fail
SELECT 1 AS a
UNION
SELECT 2 AS b
ORDER BY B

-- will run
SELECT 1 AS a
UNION
SELECT 2 AS b





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #877217
Posted Thursday, March 04, 2010 3:30 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:23 AM
Points: 304, Visits: 457
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 fail
SELECT 1 AS a
UNION
SELECT 2 AS b
ORDER 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 run
SELECT 1 AS a
UNION
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.
Post #877230
Posted Thursday, March 04, 2010 4:04 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731, Visits: 12,131
I just needed to show that your statement
u have to ensure that the aliases of everything your selecting must match between the top select and bottom select

is just wrong. Nothing more. But nothing less either.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #877256
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse