rownum and row_number() in oracle

This article mainly introduces the use of rownum and row_number() in oracle, as well as the differences and connections. It is very detailed, and friends who need it can refer to it.

row_number()over(partition by col1 order by col2) means grouping according to col1, and sorting according to col2 inside the grouping, and the value calculated by this function represents the order number after sorting within each group (consecutive and unique within the group). The difference from rownum is that when using rownum to sort, the result set is first added to the shoddy rownum and then sorted, while row_number() is to sort first and then calculate the row number after the sorting clause is included.

 

1. rownum in oracle

 

The number used for the row returned from the query, the first row returned is assigned 1, the second row is 2, and so on, this pseudo field can be used to limit the total number of rows returned by the query, and rownum cannot be specified in any table. name as a prefix.

 

1. rownum For query conditions equal to a certain value

 

If you want to find the information of the first student in the student table, you can use rownum=1 as a condition. However, if I want to find the information of the second student in the student table, I can't find the data using rownum=2. Because rownum starts from 1, but the natural numbers above 1 are considered as false conditions when rownum is equal to the judgment, so it is impossible to find rownum = n (natural numbers with n>1).
SQL> select rownum,id,name from student where rownum=1; --there is a record
SQL> select rownum,id,name from student where rownum =2; --no record

 

2. rownum for query conditions greater than a certain value

 

If you want to find the records after the second row, when rownum>2 is used, the records cannot be found, you can use the following subquery method to solve the problem. Note that rownum in the subquery must have an alias, otherwise the record will not be found. This is because rownum is not a column of a certain table. If you cannot afford an alias, you cannot know whether rownum is a column of the subquery or a column of the main query. .
SQL>select * from(select rownum no ,id,name from student) where no>2; --there is a record
SQL> select * from(select rownum,id,name from student)where rownum>2; --no record

 

3. rownum for query conditions less than a certain value

 

If you want to find the records before the third record, you can get two records when using rownum<3. Obviously rownum is considered to be true for the condition of rownum<n ((n>1 natural number), so records can be found.
SQL> select rownum,id,name from student where rownum <3; --there are records

 

4. rownum and sorting

 

The rownum in Oracle is the serial number generated when fetching data, so you must pay attention to the specified rowmun row data for the specified sorted data.
SQL> select rownum ,id,name from student order by name;

 

    ROWNUM ID NAME
---------- ------ ------------------------------- --------------------
         3 200003 Li San
         2 200002 Wang Er
         1 200001 Zhang Yi
         4 200004 Zhao Si

 

It can be seen that rownum is not a serial number generated according to the name column. The system assigns the numbers to the records in the order in which they were inserted, and the rowid is also assigned sequentially. To solve this problem, subqueries must be used

 

SQL> select rownum ,id,name from (select * from student order by name);

 

    ROWNUM ID NAME
---------- ------ ------------------------------- --------------------
         1 200003 Li San
         2 200002 Wang Er
         3 200001 Zhang Yi
         4 200004 Zhao Si

 

二、oracle中row_number()

 

1. row_number() over (order by col_1[,col_2 ...])
sorts according to col_1[,col_2 ...], returns the sorted result set, and returns a different value for each row.

 

2. row_number() over (partition by col_n[,col_m ...] order by col_1[,col_2 ...])
first group by col_n[,col_m ..., and then in each group by col_1[, col_2 ...] to sort (ascending order), and finally return the sorted result set

 

row_number() instance in oracle

 

1. Use the row_number() function for numbering, such as
select email, customerID, ROW_NUMBER() over(order by psd) as rows from QT_Customer
Principle: first sort by psd, and after sorting, number each piece of data.

 

2. Sort the order in ascending order of price, and sort each record with the following code:
select DID,customerID,totalPrice,ROW_NUMBER() over(order by totalPrice) as rows from OP_Order

 

3. Count how many times the most recent order placed by each customer is the order placed.
with tabs as 

select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order 

select MAX(rows) as 'order times',customerID from tabs group by customerID

 

4. When using windowing functions such as over, the execution of grouping and sorting in over is later than the execution of "where, group by, order by".
select  
ROW_NUMBER() over(partition by customerID order by insDT) as rows, 
customerID,totalPrice, DID 
from OP_Order where insDT>'2011-07-22'The
above code is to execute the where clause first, and after execution, give each item Records are numbered.

 

3. The difference between row_number() and rownum

 

When using rownum to sort, the result set is first added to the inferior rownum and then sorted, while row_number() is to sort first and then calculate the row number after including the sorting clause.

 

The above is the whole content of this article, I hope you like it.

 

Reprint address: http://www.jb51.net/article/65960.htm

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326240871&siteId=291194637