当前位置: 代码迷 >> SQL >> 急筛选出公司薪水拿第二的员工姓名,Select语句该如何写?多谢
  详细解决方案

急筛选出公司薪水拿第二的员工姓名,Select语句该如何写?多谢

热度:203   发布时间:2016-05-05 15:30:38.0
急:筛选出公司薪水拿第二的员工姓名,Select语句该怎么写?谢谢!
表结构:enterprise(name,salary),用select语句筛选出公司薪水拿第二的员工姓名。

------解决方案--------------------
当salary有重复时的情况
select * from
(
SELECT * , px=(SELECT COUNT(DISTINCT salary) FROM enterprise WHERE salary > = a.salary) FROM enterprise a ORDER BY px
) t
where px = 2
------解决方案--------------------
/*
先检索出不包含最大工资的结果集,然后在从结果集里去检索出最大的工资,注意我考虑到了有可能很多人工资并列第一的情况,所以用了NOT IN
*/
select * from enterprise where salary =
(
select max(salary) from
(
select salary from enterprise where name not in ( select name from enterprise where salary = (select max(salary) from enterprise ) )
)dd
)



------解决方案--------------------
select * from enterprise where salary=(select min(salary) from (select top 2 * from enterprise order by salary desc) a)
------解决方案--------------------
select * from enterprise
where salary = (
(select max(salary) from enterprise where salary < (select max(salary) from enterprise )))

------解决方案--------------------
select top 1 * from enterprice
where salary<(select max(salary) from enterprice ) 
order by salary desc
------解决方案--------------------
SELECT name,salary FROM (
SELECT *, (SELECT COUNT(*) FROM TT WHERE salary>=A.salary) AS P FROM TT A)
WHERE P=2

------解决方案--------------------
select *
from abc 
where 
code=(select max(code) 
from abc 
where code not in(select max(code) from abc))
------解决方案--------------------
SELECT TOP 1 * from (select top 2 * from enterprise b order by b.salary desc) a ORDER BY a.salary asc 
  相关解决方案