Retrieve 3rd MAX salary in Hive

可紊 提交于 2019-12-08 07:06:33

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/30488530/retrieve-3rd-max-salary-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!