Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL order by in subquery problems!

Tags:

sql

oracle

I am trying to run a subquery in Oracle SQL and it will not let me order the subquery columns. Ordering the subquery is important as Oracle seems to choose at will which of the returned columns to return to the main query.

select ps.id, ps.created_date, pst.last_updated, pst.from_state, pst.to_state,
        (select last_updated from mwcrm.process_state_transition subpst
            where subpst.last_updated > pst.last_updated
            and subpst.process_state = ps.id
            and rownum = 1) as next_response
        from mwcrm.process_state ps, mwcrm.process_state_transition pst
        where ps.created_date > sysdate - 1/24
        and ps.id=pst.process_state
        order by ps.id asc

Really should be:

select ps.id, ps.created_date, pst.last_updated, pst.from_state, pst.to_state,
        (select last_updated from mwcrm.process_state_transition subpst
            where subpst.last_updated > pst.last_updated
            and subpst.process_state = ps.id
            and rownum = 1
            order by subpst.last_updated asc) as next_response
        from mwcrm.process_state ps, mwcrm.process_state_transition pst
        where ps.created_date > sysdate - 1/24
        and ps.id=pst.process_state
        order by ps.id asc

like image 586
Kevin Parker Avatar asked Feb 25 '11 15:02

Kevin Parker


People also ask

Can we use ORDER BY in subquery in Oracle?

Order by clause does not works inside a Sub-Query.No use of giving ORDER BY clause inside the sub query. Subquery gives values to the outer query and outer query only orders the value based on the order by clause.

Can we use ORDER BY in sub query?

An ORDER BY command cannot be used in a subquery, although the main query can use an ORDER BY.

Why is ORDER BY not allowed in subqueries?

No ORDER BY is valid in a subquery when you are interested in a subset of the overall data, hence you always need a TOP (SQL Server). There's no point having an ORDER BY without TOP in a subquery because the overall ordering of the results is handled by the outer query.

Can subqueries contain group by and ORDER BY clause?

Subqueries cannot contain GROUP BY and ORDER BY clauses. Subqueries can contain ORDER BY but not the GROUP BY clause.


1 Answers

Both dcw and Dems have provided appropriate alternative queries. I just wanted to toss in an explanation of why your query isn't behaving the way you expected it to.

If you have a query that includes a ROWNUM and an ORDER BY, Oracle applies the ROWNUM first and then the ORDER BY. So the query

SELECT *
  FROM emp
 WHERE rownum <= 5
 ORDER BY empno

gets an arbitrary 5 rows from the EMP table and sorts them-- almost certainly not what was intended. If you want to get the "first N" rows using ROWNUM, you would need to nest the query. This query

SELECT *
  FROM (SELECT *
          FROM emp
         ORDER BY empno)
 WHERE rownum <= 5

sorts the rows in the EMP table and returns the first 5.

like image 78
Justin Cave Avatar answered Sep 29 '22 14:09

Justin Cave