Search code examples
oracle-databaserownum

Oracle ROWNUM pseudocolumn


I have a complex query with group by and order by clause and I need a sorted row number (1...2...(n-1)...n) returned with every row. Using a ROWNUM (value is assigned to a row after it passes the predicate phase of the query but before the query does any sorting or aggregation) gives me a non-sorted list (4...567...123...45...). I cannot use application for counting and assigning numbers to each row.


Solution

  • Is there a reason that you can't just do

    SELECT rownum, a.* 
      FROM (<<your complex query including GROUP BY and ORDER BY>>) a