If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Trouble w/ order by on a Union

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-23-04, 14:57
casgirl casgirl is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Trouble w/ order by on a Union

I am trying to order the results of 2 queries. The second query does not utilize one of the tables in the first query, so I selected a constant NULL.

I get an error when running the query. Looks like an error with the order by. The error, 'invalid identifier', points to the table name of one of the fields in the order by.


select
ta.field1,
tb.field1,
tc.field1,
where
ta.field1 = tb.field1 and
ta.field1 = tc.field1 and
some other condition
union
select
ta.field1,
NULL,
tc.field1
where
ta.field1 = tc.field1 and
some other condition
order by
ta.field1 <==== Error ''invalid identifier' points to the ta


Can I do an order by on the union of 2 queries?

Thanks,
Linn
Reply With Quote
  #2 (permalink)  
Old 07-23-04, 15:01
casgirl casgirl is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Sorry forgot the from tables.

select
ta.field1,
tb.field1,
tc.field1,
from tablea ta, tableb tb, tablec tc
where
ta.field1 = tb.field1 and
ta.field1 = tc.field1 and
some other condition
union
select
ta.field1,
NULL,
tc.field1
from tablea ta, tableb tb, tablec tc
where
ta.field1 = tc.field1 and
some other condition
order by
ta.field1 <==== Error ''invalid identifier' points to the ta
Reply With Quote
  #3 (permalink)  
Old 07-23-04, 15:07
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I just ran:
Code:
SELECT 'a' AS rsid, a.spid
   FROM master.dbo.sysprocesses AS a
UNION ALL SELECT 'b', b.spid
   FROM master.dbo.sysprocesses AS b
   ORDER BY a.spid
MS-SQL 2000 didn't complain, although I think it should have! I'll have to investigate this when I get a chance.

-PatP
Reply With Quote
  #4 (permalink)  
Old 07-23-04, 16:51
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Talking


Did you try:

Code:
ORDER BY 1

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On