How to use ORDER by clause
-
Ordinarily I would create a view to solve this problem, but I'm unable to do that in this case because of parameters supplied to the WHERE clause. I have a query;
select fielda,
fieldb,
fieldc
from viewA
where date_field between Date1 and Date2
union
select fielda,
fieldb,
fieldc,
from viewB
where date_field < Date2
union ... 3 more similar select/unionI need to have the resultant rows ordered in a particular sequence and so normally I would create a view, query the view and order the results of that query. In this instance, because of how the Date1 and Date2 are supplied, I can not use a view. Is there any way to ORDER the entire resultant set of rows when you use a union of multiple queries. Thanks. Chris
-
Ordinarily I would create a view to solve this problem, but I'm unable to do that in this case because of parameters supplied to the WHERE clause. I have a query;
select fielda,
fieldb,
fieldc
from viewA
where date_field between Date1 and Date2
union
select fielda,
fieldb,
fieldc,
from viewB
where date_field < Date2
union ... 3 more similar select/unionI need to have the resultant rows ordered in a particular sequence and so normally I would create a view, query the view and order the results of that query. In this instance, because of how the Date1 and Date2 are supplied, I can not use a view. Is there any way to ORDER the entire resultant set of rows when you use a union of multiple queries. Thanks. Chris
Use a SubQuery.
Select Field1, Field1 From (query with Unions) Order By Field1
Cheers!!!! Carlos Antollini. -
Use a SubQuery.
Select Field1, Field1 From (query with Unions) Order By Field1
Cheers!!!! Carlos Antollini.Thanks for the response, Carlos. I'm using Oracle 8I and had tried to use that but continually got errors from the parser. Once I put the brace characters around the query with the unions, it worked like a champ. None of my books even gave an example such as yours. You helped me out a lot. Thanks again. Chris