Retrieve 3rd MAX salary in Hive

2019-06-03 07:30发布

I'm a novice. I have the following Employee table.

ID  Name  Country  Salary  ManagerID

I retrieved the 3rd max salary using the following.

select name , salary From (
    select name, salary from 
    employee sort by salary desc limit 3)
    result sort by salary limit 1;

How to do the same to display 3rd max salary for each country? can we use OVER (PARTITION BY country)? I tried looking in the languageManual Windowing and Analytics but I'm finding it difficult to understand. Please help!

标签: hive hiveql
1条回答
Viruses.
2楼-- · 2019-06-03 07:50

You're definitely on the right track with windowing functions. row_number() is a good function to use here.

select name, salary
from (
  select name
    , salary
    , row_number() over (partition by country order by salary desc) idx
  from employee ) x
where idx = 3

when you order by salary, make sure that it is a numerical type, or it will not be sorted correctly.

查看更多
登录 后发表回答