Oracle ranked ranking function, ROW_NUMBER, RANK, DENSE_RANK, NTILE, Introduction

1.ROW_NUMBER()

Definitions : ROW_NUMBER () function role is to select the query to sort the data, each data plus a serial number, he can not be used for student achievement rankings, generally used for paging query, 
before such a query 10 query 10-100 student.

Examples :

1.1 pairs of student achievement ordering

Write pictures described here 
This number is the serial number of each student be desc descending according to studentScore (Score)

1.2 second to obtain performance information students

Write pictures described here 
The idea is thought paging query here used in the outer layer of the original sql then set the SELECT 
the WHERE t.number> = 1 and t.number <= 10 is not is to get the first ten student achievement information is satisfied.

2.RANK()

Definitions : RANK () function, as the name implies ranking functions, can be ranked in a field, where and why ROW_NUMBER () not the same as that, ROW_NUMBER () is sorted, when the student presence of the same grade, ROW_NUMBER () will in turn be sorted they are not the same serial number, and rank () is not the same appears the same, their rankings are the same. The following look at an example:

2.1 pairs of student achievement rankings

Write pictures described here

Here found ROW_NUMBER () and RANK () how the same? Because it is not the same student achievement ranking and sorting, like, change it below you will find the difference.

Write pictures described here

When the two are the same student achievement appears inside to change. RANK () is 122, and ROW_NUMBER () is still 123, which is the difference between RANK () and ROW_NUMBER () of the

3.DENSE_RANK()

Definitions : DENSE_RANK () function is also a ranking function, and RANK () function is similar, but also on the field rank, and that it RANK () in the end what is the difference then? Look at an example:

Examples :

Write pictures described here

DENSE_RANK () dense rank him and RANK () difference is that the ranking of continuity, DENSE_RANK () ranking is continuous, RANK () is jumping rankings, the ranking function is generally used RANK ().

4.NTILE()

Definitions : NTILE () function is the orderly distribution of the partition line to the specified number of groups, each group numbered, numbered from 1, as we said, 'zoning', like, divided into several zones, a zone how many will be.

Examples
Write pictures described here

3 times here for the first time into a 'region', the query result is a whole number, the second is divided into two regions, the query result is 112, meaning that the first 'region' is 11 two data number, the second 'area' 2 only this data.

Published 27 original articles · won praise 38 · views 20000 +

Guess you like

Origin blog.csdn.net/a1150499208/article/details/91039772