我有一个名为employee_salary
的表,其中有两列(emp_id,emp_salary)。
我需要从此表中获取第3个最低emp_salary
。在这种情况下,应该是我的查询,以便我可以得到确切的值。
答案 0 :(得分:1)
这可能是一个解决方案
select top 1 * from
(
select top 3 * from
(
select distinct emp_sal from employee order by asc emp_sal
) d orderby desc emp_sal
)
答案 1 :(得分:1)
我在Postgres数据库中对此进行了测试。我希望这个查询适用于所有类型的数据库。请试试这个。
select emp_salary from emp_salary group by emp_salary order by emp_salary limit 1 offset 2;
答案 2 :(得分:0)
SELECT TOP 1 * FROM employee_salary WHERE emp_salary in(SELECT TOP 3 emp_salary FROM employee_salary ORDER BY emp_salary)ORDER BY emp_salary DESC
但是,这并不适用于所有数据库。你需要找出替代方案。例如。在Informix中,语句将为SELECT FIRST 1 *
答案 3 :(得分:0)
使用窗口函数......此构造用于SQL Server:
;WITH CTE AS
(
SELECT ..., ROW_NUMBER() OVER (ORDER BY emp_salary) AS rn
FROM myTable
)
SELECT ...
FROM CTE
WHERE rn = 3
答案 4 :(得分:0)
您可以使用RANK () function in SQL Server
;WITH CTE AS
(
SELECT ..., RANK() OVER (ORDER BY emp_salary) AS rn
FROM myTable
)
SELECT ...
FROM CTE
WHERE rn = 3
答案 5 :(得分:0)
我通过在sql server 2008中执行以下查询得到了答案
Select MIN(emp_salary) from MyTable Where emp_salary in
(Select DISTINCT TOP 3 emp_salary from MyTable order by 1 DESC)
我得到了第3个最低值。
DISTINCT
用于当一个或多个薪水相同时。
答案 6 :(得分:0)
select * from table_name where col_name = (select (min(col_name) from table_name where col_name > (select min(col_name) from table_name where col_name > (select min(col_name) from table_name)));