Oracle SQL order by in subquery problems!

后端 未结 4 1294
终归单人心
终归单人心 2020-12-31 04:49

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

4条回答
  •  醉梦人生
    2020-12-31 05:29

    Actually "ordering" only makes sense on the outermost query -- if you order in a subquery, the outer query is permitted to scramble the results at will, so the subquery ordering does essentially nothing.

    It looks like you just want to get the minimum last_updated that is greater than pst.last_updated -- its easier when you look at it as the minimum (an aggregate), rather than a first row (which brings about other problems, like what if there are two rows tied for next_response?)

    Give this a shot. Fair warning, been a few years since I've had Oracle in front of me, and I'm not used to the subquery-as-a-column syntax; if this blows up I'll make a version with it in the from clause.

    select
        ps.id, ps.created_date, pst.last_updated, pst.from_state, pst.to_state,
        (   select min(last_updated)
            from mwcrm.process_state_transition subpst
            where subpst.last_updated > pst.last_updated
              and subpst.process_state = ps.id) as next_response
    from 
    

提交回复
热议问题