加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用oracle实现获取第N高薪水

发布时间:2020-12-12 13:43:57 所属栏目:百科 来源:网络整理
导读:编写一个 SQL 查询,获取 ? Employee ?表中第 ? n ? 高的薪水(Salary)。 +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 ? Employee ?表, n = 2 ? 时,应返回第二高的薪水 ? 200 。如果

编写一个 SQL 查询,获取?Employee?表中第?n?高的薪水(Salary)。

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

例如上述?Employee?表,n = 2?时,应返回第二高的薪水?200。如果不存在第?n?高的薪水,那么查询应返回?null

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+

?

网上有很多关于mysql的编写方法,oracle的几乎找不到,我如果有比我编写方法好的欢迎评论

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 salary,rownum as rn

from

(select DISTINCT salary from employee order by salary desc)

)

where rn = N;

RETURN result;

END;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读