Oracle's Row_number () over, rank () over and Dense_rank () over three analytic functions (RPM)

Source: Internet
Author: User

Suppose there is a student table student, the student table has a name, a score, a course number, and now I need to sort the students ' grades according to the course.

SELECT * FROM Student

1. Rank over () can be achieved on the student rankings, characterized by the same results as two are tied, as follows 1 2 2 4 5

Select Name,
Course
Rank () Over (partition by course ORDER BY score Desc) as rank
from student;

2. Dense_rank () and rank over () are similar, but the students ' grades will not be vacated by the rank of the tie, as follows 1 2 2 3 4

Select Name,
Course
Dense_rank () Over (partition by course ORDER BY score Desc) as rank
from student;

3. Row_number This function does not need to consider whether or not to tie, that is afraid to query the same value according to the same number will be continuously ranked

Select Name,
Course
Row_number () Over (partition by course ORDER BY score Desc) as rank
from student;

Answering:

1. Partition by is used to partition the result set.

2. What is the difference between partition by and group by?

Partition by simply rank the original data (the number of records is the same)

Group BY is an aggregated statistic of the raw data (the number of records may be less, one for each group)

3. When you use rank over (), the null value is the largest, and if the sort field is null, it may cause null fields to be ranked first, affecting the sorting result.

Can do this: rank over (partition by course ORDER BY score Desc nulls last)

Oracle's Row_number () over, rank () over and Dense_rank () over three analytic functions (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.