Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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
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: Yesterday @ 3:56 PM
Points: 3,852, Visits: 6,566
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)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
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: Thursday, May 5, 2016 3:59 PM
Points: 126, Visits: 615
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: Friday, April 15, 2016 10:16 PM
Points: 755, Visits: 582
Good. Now OP has many ways to order his UNION query.
Post #1505974
Posted Thursday, October 17, 2013 11:02 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 8:06 PM
Points: 42,046, Visits: 39,428
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."

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: Friday, April 15, 2016 10:16 PM
Points: 755, Visits: 582
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
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: Yesterday @ 3:56 PM
Points: 3,852, Visits: 6,566
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)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Post #1506188
Posted Friday, October 18, 2013 9:25 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 7:14 PM
Points: 8,200, Visits: 17,740
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
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: Friday, April 15, 2016 10:16 PM
Points: 755, Visits: 582
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