sql第N工资问题: leetcode 177. Nth Highest Salary

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


mysql解法:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      # Write your MySQL query statement below.
     select MIN(t3.salary) from (
      select case when @salary = salary then   @rank else @rank := @rank+1 end   as rank ,@salary :=  t1.salary AS salary  
    from  ((
      select t.id,t.salary  
          from employee t
        order by t.salary desc
  ) t1,(select @rank := 0) t2,(select @salary := 0) t4)
       
      ) t3 
      where  t3.rank = n
  );
END

oracle解法:

CREATE FUNCTION getNthHighestSalary(N IN NUMBER) RETURN NUMBER IS
result NUMBER;
BEGIN
    with p as (
    select t.id,t.salary ,dense_rank()over(order by t.salary desc) rn
        from employee t
 )
    select max(t1.salary) 
    into result
    from p t1
    where t1.rn = n;
    RETURN result;
    
END;

重点:sql的连续排序实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值