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 @ 5:31 PM
Points: 1,977, Visits: 2,926
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: Tuesday, July 22, 2014 1:16 PM
Points: 107, Visits: 511
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: 2 days ago @ 2:58 AM
Points: 718, Visits: 543
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 @ 5:34 PM
Points: 36,793, Visits: 31,250
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: 2 days ago @ 2:58 AM
Points: 718, Visits: 543
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 @ 5:31 PM
Points: 1,977, Visits: 2,926
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 @ 3:37 PM
Points: 3,374, Visits: 7,300
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: 2 days ago @ 2:58 AM
Points: 718, Visits: 543
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