HiveQL从员工表中查找第二大薪水?

时间:2015-12-18 10:49:17

标签: hive hiveql

如何使用HiveQl从员工表中查找第二大薪水?

3 个答案:

答案 0 :(得分:2)

    SELECT * from(select salary,row_number( ) 
    over (order by salary desc ) 
    as BLAH_no from table where group by salary) T where T.BLAH_no=2;

答案 1 :(得分:2)

如果是平局,接受的答案将无效。因此,以下是我的代码,它适用于所有情况。只需将row_number替换为dense_rank即可。想进一步了解dense_rank,然后访问此link

select * from  (SELECT dep_name,salary,DENSE_RANK() over(ORDER BY salary desc) as rank FROM department) as A where rank = 2;

输出:

+--------+------+----------+
|dep_name|salary|     rank |
+--------+------+----------+
|      CS| 30000|         2|
|   CIVIL| 30000|         2|
+--------+------+----------+  

希望有帮助!

答案 2 :(得分:0)

我自己得到了答案。下面是从Employee表中获取第二个最高工资的HQL查询。

select firstname,salary from
    (select firstname, salary from employee sort by salary desc limit 2)
     result sort by salary limit 1;

快乐学习!!