Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

rank,dense_rank and row_number in Analytical Functions Hive

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
  • User AvatarKiran Dalvi
  • 17 Dec, 2021
  • 0 Comments
  • 2 Mins Read

rank,dense_rank and row_number in Analytical Functions Hive

sample data

Analytical data sample

Load sample data to hive table

1
2
3
4
5
6
7
8
9
10
CREATE EXTERNAL TABLE IF NOT EXISTS users.emp
(id int,
name STRING,
department string,
salary STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 'hdfs://localhost:9000/user/hive/input/users/'
TBLPROPERTIES("skip.header.line.count"="1");

RANK() Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
select name,department,salary,rank() over (partition by department order by salary desc) as salary_rank from users.emp;
 
name department salary salary_rank
Warren Rumble Accounting 60000 1
Warren Rumble Accounting 60000 1
Warren Rumble Accounting 60000 1
Rosella Tomaszek Accounting 50000 4
Nike Lighton Accounting 50000 4
Rosella Tomaszek Accounting 50000 4
Nike Lighton Accounting 50000 4
Rosella Tomaszek Accounting 50000 4
Nike Lighton Accounting 50000 4
Sherri Eckh Accounting 20000 10
Sherri Eckh Accounting 20000 10
Sherri Eckh Accounting 20000 10
Waylin Knoton Accounting 10000 13
Waylin Knoton Accounting 10000 13
Waylin Knoton Accounting 10000 13
Vanni Sandall Marketing 50000 1
Vanni Sandall Marketing 50000 1
Vanni Sandall Marketing 50000 1
Lettie Diloway Marketing 30000 4
Lemar Speck Marketing 30000 4
Lemar Speck Marketing 30000 4
Lettie Diloway Marketing 30000 4
Othilia Timmons Marketing 30000 4
Lemar Speck Marketing 30000 4
Lettie Diloway Marketing 30000 4
Othilia Timmons Marketing 30000 4
Othilia Timmons Marketing 30000 4
Thurston Doumenc Marketing 20000 13
Thurston Doumenc Marketing 20000 13
Thurston Doumenc Marketing 20000 13
Sagar Product Management 50000 1
Sagar Product Management 50000 1
Steffane Davenport Product Management 50000 1
Sagar Product Management 50000 1
Steffane Davenport Product Management 50000 1
Steffane Davenport Product Management 50000 1
Ankush Product Management 20000 7
Ankush Product Management 20000 7
Ankush Product Management 20000 7
Time taken: 20.325 seconds, Fetched: 39 row(s)
hive>

DENSE_RANK() Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
select name,department,salary,dense_rank() over (partition by department order by salary desc) as salary_rank from users.emp;
 
name    department      salary  salary_rank
Warren Rumble   Accounting      60000   1
Warren Rumble   Accounting      60000   1
Warren Rumble   Accounting      60000   1
Rosella Tomaszek        Accounting      50000   2
Nike Lighton    Accounting      50000   2
Rosella Tomaszek        Accounting      50000   2
Nike Lighton    Accounting      50000   2
Rosella Tomaszek        Accounting      50000   2
Nike Lighton    Accounting      50000   2
Sherri Eckh     Accounting      20000   3
Sherri Eckh     Accounting      20000   3
Sherri Eckh     Accounting      20000   3
Waylin Knoton   Accounting      10000   4
Waylin Knoton   Accounting      10000   4
Waylin Knoton   Accounting      10000   4
Vanni Sandall   Marketing       50000   1
Vanni Sandall   Marketing       50000   1
Vanni Sandall   Marketing       50000   1
Lettie Diloway  Marketing       30000   2
Lemar Speck     Marketing       30000   2
Lemar Speck     Marketing       30000   2
Lettie Diloway  Marketing       30000   2
Othilia Timmons Marketing       30000   2
Lemar Speck     Marketing       30000   2
Lettie Diloway  Marketing       30000   2
Othilia Timmons Marketing       30000   2
Othilia Timmons Marketing       30000   2
Thurston Doumenc        Marketing       20000   3
Thurston Doumenc        Marketing       20000   3
Thurston Doumenc        Marketing       20000   3
Sagar   Product Management      50000   1
Sagar   Product Management      50000   1
Steffane Davenport      Product Management      50000   1
Sagar   Product Management      50000   1
Steffane Davenport      Product Management      50000   1
Steffane Davenport      Product Management      50000   1
Ankush  Product Management      20000   2
Ankush  Product Management      20000   2
Ankush  Product Management      20000   2
Time taken: 20.13 seconds, Fetched: 39 row(s)
hive>

ROW_NUMBER()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
select name,department,salary,row_number() over (partition by department order by salary desc) as salary_rank from users.emp;
 
name    department      salary  salary_rank
Warren Rumble   Accounting      60000   1
Warren Rumble   Accounting      60000   2
Warren Rumble   Accounting      60000   3
Rosella Tomaszek        Accounting      50000   4
Nike Lighton    Accounting      50000   5
Rosella Tomaszek        Accounting      50000   6
Nike Lighton    Accounting      50000   7
Rosella Tomaszek        Accounting      50000   8
Nike Lighton    Accounting      50000   9
Sherri Eckh     Accounting      20000   10
Sherri Eckh     Accounting      20000   11
Sherri Eckh     Accounting      20000   12
Waylin Knoton   Accounting      10000   13
Waylin Knoton   Accounting      10000   14
Waylin Knoton   Accounting      10000   15
Vanni Sandall   Marketing       50000   1
Vanni Sandall   Marketing       50000   2
Vanni Sandall   Marketing       50000   3
Lettie Diloway  Marketing       30000   4
Lemar Speck     Marketing       30000   5
Lemar Speck     Marketing       30000   6
Lettie Diloway  Marketing       30000   7
Othilia Timmons Marketing       30000   8
Lemar Speck     Marketing       30000   9
Lettie Diloway  Marketing       30000   10
Othilia Timmons Marketing       30000   11
Othilia Timmons Marketing       30000   12
Thurston Doumenc        Marketing       20000   13
Thurston Doumenc        Marketing       20000   14
Thurston Doumenc        Marketing       20000   15
Sagar   Product Management      50000   1
Sagar   Product Management      50000   2
Steffane Davenport      Product Management      50000   3
Sagar   Product Management      50000   4
Steffane Davenport      Product Management      50000   5
Steffane Davenport      Product Management      50000   6
Ankush  Product Management      20000   7
Ankush  Product Management      20000   8
Ankush  Product Management      20000   9
Time taken: 22.099 seconds, Fetched: 39 row(s)