⌘BABAKOV.NET Let's code!
100% [light] [ru]

Oracle - RANK() and DENSE_RANK() Functions (Or Learning to Choose Necessary Values Within a Multiple Selection)

Let's start with the RANK() function. With explanations for what it is and in what cases it may be useful.

The RANK() function is a very useful function that allows us to number a set by some grouping value within the entire selected set. It is easiest to show this with an example. To do this, let's create a small table:

ID	PERSON	DT			SM

1	Роман	01.10.2016 11:51:31	545.3
2	Роман	01.10.2016 11:51:31	445.2
3	Роман	01.10.2016 11:51:31	145.3
4	Кирил	01.05.2016 16:51:31	99.5
5	Алена	01.07.2016 12:51:31	445.3
6	Роман	01.12.2016 16:51:31	876.1
7	Кирил	01.06.2016 16:51:31	237.22
8	Алена	01.12.2016 16:51:31	145.3
9	Алена	01.02.2016 14:51:31	534.7
10	Роман	01.03.2016 16:51:31	165.3
11	Кирил	01.04.2016 16:51:31	345.2


Let's assume that this table contains some issued bills to different users PERSON for different amounts SM at different times DT.

Now, let's assume that we want to get the maximum amount issued to each user for all time.
It is exactly this kind of task that the RANK() function allows us to solve very easily.

To achieve the desired result, we need to group users by salary in descending order, like this:

select * from T_PAYS t order by t.PERSON, t.SM desc

We will get:

ID	PERSON	DT			SM

9	Алена	01.02.2016 14:51:31	534.7 --Максимальное значение для Алены
5	Алена	01.07.2016 12:51:31	445.3
8	Алена	01.12.2016 16:51:31	145.3
11	Кирил	01.04.2016 16:51:31	345.2 --Максимальное значение для Кирила
7	Кирил	01.06.2016 16:51:31	237.22
4	Кирил	01.05.2016 16:51:31	99.5
6	Роман	01.12.2016 16:51:31	876.1 --Максимальное значение для Романа
1	Роман	01.10.2016 11:51:31	545.3
2	Роман	01.10.2016 11:51:31	445.2
10	Роман	01.03.2016 16:51:31	165.3
3	Роман	01.10.2016 11:51:31	145.3



Now, all we need to do is take the first record for each user in order, and it will be the maximum value. And for this, we will need the RANK() function.
For this, we will rewrite our query like this:

select t.*, rank() over(partition by t.PERSON order by t.sm desc) rnk from T_PAYS t

We will get:

ID	PERSON	DT			SM	RNK

9	Алена	01.02.2016 14:51:31	534.7	1
5	Алена	01.07.2016 12:51:31	445.3	2
8	Алена	01.12.2016 16:51:31	145.3	3
11	Кирил	01.04.2016 16:51:31	345.2	1
7	Кирил	01.06.2016 16:51:31	237.22	2
4	Кирил	01.05.2016 16:51:31	99.5	3
6	Роман	01.12.2016 16:51:31	876.1	1
1	Роман	01.10.2016 11:51:31	545.3	2
2	Роман	01.10.2016 11:51:31	445.2	3
10	Роман	01.03.2016 16:51:31	165.3	4
3	Роман	01.10.2016 11:51:31	145.3	5



Now we have an additional column that numbers the values by the sum (SM) within the groupings for each person (PERSON).

Now all that remains is to select from the resulting set those records where the value of the new field RNK = 1.

select * from 
(
select t.*, rank() over(partition by t.PERSON order by t.sm desc) rnk from T_PAYS t 
)
where rnk=1

We will get the desired set of maximum bills for each user:

ID	PERSON	DT			SM	RNK

9	Алена	01.02.2016 14:51:31	534.7	1
11	Кирил	01.04.2016 16:51:31	345.2	1
6	Роман	01.12.2016 16:51:31	876.1	1



Now let's try to explain why we need the DENSE_RANK() function. In fact, these functions do the same thing, with a very small exception, which we will demonstrate with an example. To do this, let's change the original set so that it has many identical values of the sum of the bill (SM). Like this:

ID	PERSON	DT			SM

9	Алена	01.02.2016 14:51:31	534.7
5	Алена	01.07.2016 12:51:31	445.3
8	Алена	01.12.2016 16:51:31	145.3
11	Кирил	01.04.2016 16:51:31	345.2
7	Кирил	01.06.2016 16:51:31	237.22
4	Кирил	01.05.2016 16:51:31	237.22
6	Роман	01.12.2016 16:51:31	876.1
2	Роман	01.10.2016 11:51:31	545.3
1	Роман	01.10.2016 11:51:31	545.3
10	Роман	01.03.2016 16:51:31	145.3
3	Роман	01.10.2016 11:51:31	145.3



Now, let's perform the familiar script, adding another column that will number the groupings, but with the DENSE_RANK() function:

select t.*, rank() over(partition by t.PERSON order by t.sm desc) rnk
, dense_rank() over(partition by t.PERSON order by t.sm desc) dense_rnk
from T_PAYS t 

We will get the result:

ID	PERSON	DT			SM	RNK	DENSE_RNK

9	Алена	01.02.2016 14:51:31	534.7	1	1
5	Алена	01.07.2016 12:51:31	445.3	2	2
8	Алена	01.12.2016 16:51:31	145.3	3	3
11	Кирил	01.04.2016 16:51:31	345.2	1	1
7	Кирил	01.06.2016 16:51:31	237.22	2	2
4	Кирил	01.05.2016 16:51:31	237.22	2	2
6	Роман	01.12.2016 16:51:31	876.1	1	1
2	Роман	01.10.2016 11:51:31	545.3	2	2
1	Роман	01.10.2016 11:51:31	545.3	2	2
10	Роман	01.03.2016 16:51:31	145.3	4	3
3	Роман	01.10.2016 11:51:31	145.3	4	3


Now we can see the difference for person Roman, here it is clear that the RANK() function assigns a ranking number to the next record within the groupings for the field SM, while the DENSE_RANK() function assigns simply the next ranking number. This difference may be useful to us when solving a task where we need to select not the first record, but the 3rd or 4th, and here the condition for applying one or the other function may vary depending on what we want to get. If we want to get the 3rd unique value, it is better to use DENSE_RANK(), with RANK() we can get the ranking number of the record of the sought value.

That's all.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.