简体   繁体   English

SQL:SyntaxError:使用row_number()函数排序

[英]SQL: SyntaxError: order by with row_number() function

I have the following table Employee : 我有下表Employee

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

I am using the following code to find the second highest salary: 我正在使用以下代码来查找第二高的薪水:

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

However, I get an error: 但是,我得到一个错误:

SyntaxError: near 't as ( 语法错误:靠近't as(
select Salary, row_number() over (order by Salary desc) as salary_ord' 选择Salary,row_number()超过(按Salary desc排序)作为salary_ord'

What did I do wrong here? 我在这里做错了什么? Thanks! 谢谢!

In SQL, the correct comparison operator is = , not == . 在SQL中,正确的比较运算符是= ,而不是== So, this is the ANSI SQL version of your query: 因此,这是查询的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;

However, your error suggests that your database doesn't support with or window functions. 但是,您的错误表明您的数据库不支持with或window函数。

In SQL Server, 在SQL Server中,

You can do: 你可以做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM