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
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:25 PM
Points: 2,044, Visits: 3,059
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)
"In America, every man is innocent until proven broke!" Brant Parker
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: Saturday, August 16, 2014 8:16 AM
Points: 107, Visits: 513
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: Tuesday, August 12, 2014 12:24 AM
Points: 718, Visits: 545
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: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(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: Tuesday, August 12, 2014 12:24 AM
Points: 718, Visits: 545
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
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:25 PM
Points: 2,044, Visits: 3,059
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)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1506188
Posted Friday, October 18, 2013 9:25 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:07 PM
Points: 3,559, Visits: 7,679
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.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

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: Tuesday, August 12, 2014 12:24 AM
Points: 718, Visits: 545
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