Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Problem sorting results of a Union query passed to Oracle

Re: Problem sorting results of a Union query passed to Oracle

From: Mark Gumbs <mgumbs_at_nospam.hotmail.com>
Date: Mon, 21 Jun 1999 16:16:52 +0100
Message-ID: <376e5511.0@145.227.194.253>


You cannot use an order by with a union statement. Try enclosing your statement in brackets.

(
  SELECT * FROM logfile_h WHERE (ssn = '123456789') UNION SELECT * FROM   logfile WHERE (ssn = '123456789')
)
ORDER BY login_timestamp

Even using this method, the column you are ordering by needs to be in your sql statement.

e.g

SQL> l
  1 (select * from emp where empno=7934   2 union
  3 select * from emp where empno=7902)   4* order by empno
SQL> /
order by empno

         *
ERROR at line 4:
ORA-00904: invalid column name

But this works

SQL> (select empno, ename from emp where empno=7934   2 union
  3 select empno, ename from emp where empno=7902)   4 order by empno;

    EMPNO ENAME
--------- ----------

     7902 FORD
     7934 MILLER


HTH, Mark

David wrote in message <376e51a5_at_news2.one.net>...
>I'm having a problem sorting the result of a UNION query. I'm using
Oracle'
>s built in OLE controls within Visual Basic to pass the following statement
>when creating a dynaset.
>
>SELECT * FROM logfile_h WHERE (ssn = '123456789') UNION SELECT * FROM
>logfile WHERE (ssn = '123456789') ORDER BY login_timestamp
>
>When I run this query, Oracle returns "Invalid Column Name". The query
>runs fine without the ORDER BY clause. It also runs fine if I use the
ORDER
>BY with only part of the select as in "SELECT * FROM logfile_h WHERE (ssn =
>'123456789') ORDER BY login_timestamp".
>
>Any input appreciated.
>
>Dave B.
>
>
>
Received on Mon Jun 21 1999 - 10:16:52 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US