Oracle SQL order by in subquery problems!

后端 未结 4 1323
终归单人心
终归单人心 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:18

    I've experienced this myself and you have to use ROW_NUMBER(), and an extra level of subquery, instead of rownum...

    Just showing the new subquery, something like...

    (
      SELECT
        last_updated
      FROM
      (
        select
          last_updated,
          ROW_NUMBER() OVER (ORDER BY last_updated ASC) row_id
        from
          mwcrm.process_state_transition subpst
        where
          subpst.last_updated > pst.last_updated
          and subpst.process_state = ps.id
      )
        as ordered_results
      WHERE
        row_id = 1
    )
      as next_response
    


    An alternative would be to use MIN instead...

    (
      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
    

提交回复
热议问题