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 ««12

UNION Query help Expand / Collapse
Author
Message
Posted Thursday, October 17, 2013 4:11 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 1,757, Visits: 2,576
Or just:

ORDER BY 1

I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot :


SQL DBA,SQL Server MVP('07, '08, '09)
I'm not fat, I'm gravity challenged.
Post #1505938
Posted Thursday, October 17, 2013 9:18 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 11:29 PM
Points: 104, Visits: 495
Parentheses
(SELECT ISNULL(CboValuesTranslations.cboValueTranslationName,ISNULL(CboValues.cboValueName,'')) as Name,
cboValues.cboValueSqlId as SqlId, CboValues.cboValueIncId as IncId,
LTRIM(STR(cboValues.cboValueSqlId))+'-'+LTRIM(STR(CboValues.cboValueIncId)) AS KeyId


FROM ListsOfCboValues INNER JOIN CboValues ON ListsOfCboValues.listOfCboValueSqlId = CboValues.listOfCboValueSqlId AND ListsOfCboValues.listOfCboValueIncId = CboValues.listOfCboValueIncId AND CboValues.isDeleted=0x0 LEFT OUTER JOIN CboValuesTranslations ON cboValuesTranslations.cboValueSqlId = cboValues.cboValueSqlId AND cboValuesTranslations.cboValueIncId = cboValues.cboValueIncId AND cboValuesTranslations.isDeleted=0x0 AND cboValuesTranslations.LanguageSqlId=2 AND cboValuesTranslations.LanguageIncId=2
WHERE ListsOfCboValues.isDeleted = 0x0 AND (ListsOfCboValues.listOfCboValueCode = 'EAS-00008')


UNION

SELECT 'NULL' as Name,
null as SqlId, null as IncId,
'NULL' AS KeyId)

ORDER BY 1 Asc

Post #1505965
Posted Thursday, October 17, 2013 10:50 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:20 PM
Points: 715, Visits: 520
Good. Now OP has many ways to order his UNION query.
Post #1505974
Posted Thursday, October 17, 2013 11:02 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:52 PM
Points: 36,016, Visits: 30,304
ScottPletcher (10/17/2013)
Or just:

ORDER BY 1

I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot :


Thank goodness that method has finally been deprecated.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1505976
Posted Thursday, October 17, 2013 11:06 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:20 PM
Points: 715, Visits: 520
Jeff Moden (10/17/2013)
ScottPletcher (10/17/2013)
Or just:

ORDER BY 1

I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot :


Thank goodness that method has finally been deprecated.



+1 for Jeff Moden
Post #1505980
Posted Friday, October 18, 2013 8:22 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 1,757, Visits: 2,576
T.Ashish (10/17/2013)
Jeff Moden (10/17/2013)
ScottPletcher (10/17/2013)
Or just:

ORDER BY 1

I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot :


Thank goodness that method has finally been deprecated.



+1 for Jeff Moden



Why? Just makes it more difficult to order complex columns and/or UNIONed statements.


SQL DBA,SQL Server MVP('07, '08, '09)
I'm not fat, I'm gravity challenged.
Post #1506188
Posted Friday, October 18, 2013 9:25 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 2,784, Visits: 5,965
ScottPletcher (10/18/2013)
T.Ashish (10/17/2013)
Jeff Moden (10/17/2013)
ScottPletcher (10/17/2013)
Or just:

ORDER BY 1

I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot :


Thank goodness that method has finally been deprecated.



+1 for Jeff Moden



Why? Just makes it more difficult to order complex columns and/or UNIONed statements.

Is as simple as using column aliases from first query.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1506225
Posted Sunday, October 20, 2013 12:49 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:20 PM
Points: 715, Visits: 520
Luis Cazares (10/18/2013)
ScottPletcher (10/18/2013)
T.Ashish (10/17/2013)
Jeff Moden (10/17/2013)
ScottPletcher (10/17/2013)
Or just:

ORDER BY 1

I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot :


Thank goodness that method has finally been deprecated.



+1 for Jeff Moden



Why? Just makes it more difficult to order complex columns and/or UNIONed statements.

Is as simple as using column aliases from first query.


Even I have never used ordinal position ever, so I'm glad its deprecated.
Post #1506444
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse