dbTalk Databases Forums  

Error message on using union statement with order by using reserved words

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Error message on using union statement with order by using reserved words in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
signon77
 
Posts: n/a

Default Error message on using union statement with order by using reserved words - 10-30-2007 , 04:08 AM






Hello,

The part of my stored procedure giving me problems is this:
************************************************** ************************************************** *******************************
---------------------------------
-- FINAL OUTPUT - Two Record Sets
---------------------------------
--
-- Trades excluding trades with a corresponding REV/REPs
--
SELECT
d.BookName,
d.ExternalId, -- Deal
d.D1MTM, -- MTM on d-1
d.PoolMTM, -- Pool MTM
d.[Difference], -- Difference
d.Comments -- Comments
FROM
#AllDeals d
WHERE
d.ExternalId NOT IN
(SELECT ExternalID FROM #RevReps
UNION SELECT ExternalID FROM #RevRepDeals)
order by bookname asc, abs([Difference]) desc
--
-- Rev/reps and correspondimg REV/REP unwinds
--
SELECT
rr.BookName,
rr.ExternalId, -- Deal
rr.D1MTM, -- MTM on d-1
rr.PoolMTM, -- Pool MTM
rr.[Difference], -- Difference
rr.Comments -- Comments
FROM
#RevReps rr
UNION SELECT
d.BookName,
d.ExternalId, -- Deal
d.D1MTM, -- MTM on d-1
d.PoolMTM, -- Pool MTM
d.[Difference], -- Difference
d.Comments -- Comments
FROM
#RevRepDeals d
ORDER BY
ExternalId desc, Bookname asc, abs([Difference]) desc

************************************************** ************************************************** *************

I am trying to order the results of the UNIONs by the absolute value
in the column Difference. 'Diffference' is a reserved word which is
why it appears in square brackets. However every time I run this
stored procedure I get the following error message:

"ORDER BY items must appear in the select list if the statement
contains a UNION operator"

As 'Difference' is clearly being selected why am I getting this error
message?

Rob


Reply With Quote
  #2  
Old   
Roy Harvey (SQL Server MVP)
 
Posts: n/a

Default Re: Error message on using union statement with order by using reserved words - 10-30-2007 , 05:45 AM






On Tue, 30 Oct 2007 03:08:23 -0700, signon77 <signon77 (AT) yahoo (DOT) com>
wrote:

Quote:
ORDER BY
ExternalId desc, Bookname asc, abs([Difference]) desc

************************************************** ************************************************** *************

I am trying to order the results of the UNIONs by the absolute value
in the column Difference. 'Diffference' is a reserved word which is
why it appears in square brackets. However every time I run this
stored procedure I get the following error message:

"ORDER BY items must appear in the select list if the statement
contains a UNION operator"

As 'Difference' is clearly being selected why am I getting this error
message?
Because what you are trying to ORDER BY is an expression,
abs([Difference]), not a column in the result set. You can get away
with an expression with a single SELECT, but not when using UNION.

To get that order you would have to either add a column with that
expression to each SELECT in the UNION, or place the entire query
inside a derived table and ORDER BY in the outer query:

SELECT *
FROM (<your query here without ORDER BY>)
ORDER BY ExternalId desc, Bookname asc, abs([Difference]) desc

Roy Harvey
Beacon Falls, CT


Reply With Quote
  #3  
Old   
signon77
 
Posts: n/a

Default Re: Error message on using union statement with order by using reserved words - 10-30-2007 , 10:44 AM



Hi Roy,

Thanks for your help with this. My code now works looking like this:


SELECT
rr.BookName,
rr.ExternalId, -- Deal
rr.D1MTM, -- MTM on d-1
rr.PoolMTM, -- Pool MTM
abs(rr.[Difference]), -- Difference
rr.Comments -- Comments
FROM
#RevReps rr
UNION SELECT
d.BookName,
d.ExternalId, -- Deal
d.D1MTM, -- MTM on d-1
d.PoolMTM, -- Pool MTM
abs(d.[Difference]), -- Difference
d.Comments -- Comments
FROM
#RevRepDeals d
ORDER BY ExternalId desc, BookName asc, abs([Difference]) desc


Thanks again!!

Robert Ilechuku


Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: Error message on using union statement with order by using reserved words - 10-31-2007 , 08:07 AM



Quote:
I am trying to order the results of the UNIONs by the absolute value
in the column Difference. 'Difference' is a reserved word which is
why it appears in square brackets. <<

The Standard SQL convention is to use double quotes, not proprietary
brackets. Likewise, the Standard SQL convention is that columns in a
UNION result do not have names; you have to give them names in an AS
clause. Finally, the Standard SQL convention is that the ORDER BY
clause reference column names in the SELECT clause of the cursor, not
expressions.

SELECT X.book_name, X.external_id, X.d1mtm, X.poolmtm,
X.difference_abs, X.comments
FROM (#Revreps
UNION
SELECT book_name, external_id, d1mtm, poolmtm,
ABS("difference"), comments
FROM #RevrepDeals
) AS X (book_name, external_id, d1mtm, poolmtm, difference_abs,
comments)
ORDER BY external_id DESC, book_name ASC, difference_abs DESC;

A little minor effort and you have portable SQL instead of dialect!



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.