简体   繁体   中英

row_number() function in oracle

I am using ROW_NUMBER function in oracle and trying to understand how it is going to behave when the partition by and order by clause holds the same data then how ranking will work (if there are duplicate records).

below is the sample dataset

select * from test

Result

Dept        salary  created date    
HR          500     25-Jul          
HR          200     25-Jul          
HR          500     26-Jul          
Accounts    300     25-Jan          
Accounts    300     26-Jan          
Accounts    300     27-Jan          

i ran the row_number function based on above set

select *,ROW_NUMBER() OVER(partition by Dept order by salary) as row_number
from test

result

Dept        salary  created date    row_number
HR          500     25-Jul          1
HR          200     25-Jul          1
HR          500     26-Jul          2
Accounts    300     25-Jan          1
Accounts    300     26-Jan          2
Accounts    300     27-Jan          3

As you can see the output above, i am using the Dept as partition by and salary as order by for row_number, it gave me the ranking 1,2,3. I am trying to understand here is that for the same data in the partition by and order by clause, does oracle assign the row_number based on when record entered into the system like in above "Accounts" "300" it gave the row_number 1 for the record which entered earliest in the system "25-Jan"

is there anywhere it is clearly mentioned that if it is doing partition by and order by on same data then ranking will be done based on when those records entered into the system.

I am trying to understand here is that for the same data in the partition by and order by clause, does oracle assign the row_number based on when record entered into the system like in above "Accounts" "300"

No, it does not. SQL tables represent unordered sets. There is no ordering, unless provided by explicitly by referring to column values.

If you are sorting by values that are the same, there is no guarantee on the ordering of the rows . Note that running the same query twice can produce different results when there are ties in order by keys. It is even possible within the same query. This is true both for the order by clause and for analytic functions.

If you want a guarantee, then you need to include a unique column as the last sorting key (well, it could not be the last, but it would effectively be the last one).

I guess you end result can be achieved using ROWID pseudocolumn as ROWID only generated when data entered into system -

SELECT T.*,ROW_NUMBER() OVER(partition by Dept order by salary, ROWID) as row_number
FROM test T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM