Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ORDER BY problem

Status
Not open for further replies.

ColinM

Programmer
Jun 29, 2000
189
TH
Got a query like the following:

use pubs
select state from publishers
union all
select state from authors
order by (case authors.state when 'UT' then 1 else 2 end),authors.state

Which should order states with 'UT' first. It works fine on just the authors table, but when the union is introduced I get an error:

The column prefix 'authors' does not match with a table name or alias name used in the query.

:-(
 

I've tried it without table names or aliases and get this error:

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

Even thought "state" appears in both select lists? (or even when just in one)

Any other ideas? I'm stumped (again!)
 
i try run your query and it seems you try to extract 'UT'
from author which doesn't found in publishers (am i correct?). so i change it to the one below:

select ordrid=2,state from publishers
union all
select ordrid=(case when state = 'UT' then 1 else 2 end),state from authors
order by ordrid,state

provided, 'UT' is not found in publishers. I know the 'ordrid' is a bit annoying when you just want 'state' field but i can't seems to get rid of it.

unless someone knows better...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top