leetcode - database - 177. Nth Highest Salary (Oracle)

题目链接:https://leetcode.com/problems/nth-highest-salary/description/

题意:查询出表中工资第N高的值

 思路:

1、先按照工资从高到低排序(注意去重)

select distinct Salary from Employee order by Salary desc

  

2、使用rownum给查询出的数据标注行号

select rownum ro, s.Salary from (select distinct Salary from Employee order by Salary desc)s

  

3、查询行号>=N并且<=N(即N位)的值,注意在oracle函数中 修改查询出的字段名要用into

CREATE FUNCTION getNthHighestSalary(N IN NUMBER) RETURN NUMBER IS
result NUMBER;
BEGIN
    /* Write your PL/SQL query statement below */
    select Salary into result from (select rownum ro, s.Salary from (select distinct Salary from Employee order by Salary desc)s) where ro>=N and ro<=N;
    RETURN result;
END;

  

posted @ 2018-09-17 15:24  非我非非我  阅读(347)  评论(0编辑  收藏  举报