SQL:SyntaxError:order by row_number()函数

时间:2017-09-03 05:15:26

标签: sql row-number sql-order-by

我有下表Employee

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 150    |
| 2  | 290    |
| 3  | 302    |
+----+--------+

我使用以下代码找到第二高薪:

with t as 
(
    select 
        Salary, 
        row_number() over (order by Salary desc) as salary_ord 
    from 
        Employee
)
select Salary 
from t 
where salary_ord == 2

然而,我收到错误:

  

SyntaxError:near&#t; t as(
         选择Salary,row_number()over(Salary desc的顺序)作为salary_ord'

我在这里做错了什么?谢谢!

2 个答案:

答案 0 :(得分:2)

在SQL中,正确的比较运算符为=,而不是==。因此,这是查询的ANSI SQL版本:

with t as (
      select Salary, row_number() over (order by Salary desc) as salary_ord 
      from Employee
     )
select Salary
from t
where salary_ord = 2;

但是,您的错误表明您的数据库不支持with或窗口函数。

答案 1 :(得分:0)

在SQL Server中,

你可以这样做:

select top 1 Salary 
from Employee 
order by Salary desc
offset 1 row fetch next 1 row only