Home » SQL & PL/SQL » SQL & PL/SQL » Order by with WITH clause (Oracle 10g)
Order by with WITH clause [message #633851] Thu, 26 February 2015 04:38 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

The 1st code is working fine but 2nd code is not working .
WITH temp AS (SELECT * FROM emp)
SELECT * FROM (SELECT temp.*,row_number() OVER(PARTITION BY JOB ORDER BY JOB,hiredate) rn FROM temp)
WHERE rn=1
UNION ALL
SELECT temp.*,1 FROM temp ORDER BY 3,5;


WITH temp AS (SELECT * FROM emp)
SELECT * FROM (SELECT temp.*,row_number() OVER(PARTITION BY JOB ORDER BY JOB,hiredate) rn FROM temp)
WHERE rn=1
UNION ALL
select temp.*,1 from temp order by JOB,HIREDATE;


Regards,
Nathan
Re: Order by with WITH clause [message #633854 is a reply to message #633851] Thu, 26 February 2015 05:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This is has nothing to do with WITH.
This is SQL, you cannot name the columns in ORDER BY with a composite statement, you have to use column position.

SQL> select * from emp
  2  intersect
  3  select * from emp
  4  order by ename
  5  /
order by ename
         *
ERROR at line 4:
ORA-00904: "ENAME": invalid identifier

Re: Order by with WITH clause [message #633855 is a reply to message #633851] Thu, 26 February 2015 05:10 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Or, project the columns explicitly:
orclz> select * from dept union select * from dept order by dname;
select * from dept union select * from dept order by dname
                                                     *
ERROR at line 1:
ORA-00904: "DNAME": invalid identifier


orclz> select deptno,dname,loc from dept union select deptno,dname,loc from dept order by dname;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        40 OPERATIONS     BOSTON
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO

orclz>

Re: Order by with WITH clause [message #633856 is a reply to message #633855] Thu, 26 February 2015 05:31 Go to previous message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank you very much for clarification.
Previous Topic: How to handle unknown number of variables
Next Topic: How do I use SQL column values as column names
Goto Forum:
  


Current Time: Fri Apr 26 19:23:35 CDT 2024