--题目:Employee表中有ID,Name,Salary三列,求薪资排序第二高的Employee的Name
select * FROM [Employee]--等于2时为空,因为有并列第一
SELECT name from (select Name, RANK() over (order by salary desc) as rankIndex
FROM [Test].[dbo].[Employee]) as temp
where temp.rankIndex=2-- 先找出前两高,然后找出第二高
select name from Employee where Salary=(select top 1 salary from (select distinct top 2 salary from Employee order by Salary desc) as temp1 order by Salary)-- 借助Max函数,先找出最大,然后在在排除最大的数据量找最大
select Name from [Employee] where Salary =(
select MAX(Salary) from [Employee] where Salary not in (select MAX(Salary) from [Employee]))--先找出前两高,然后借助min函数找出最小值,即为第二高
select Name from [Employee] where Salary =(
select min(Salary) from (select distinct top 2 salary from Employee order by Salary desc) as temp1 )
本文发布于:2024-01-31 15:29:35,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170668617829522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |