3

我正在尝试运行此查询

    SELECT        Name, Position, Salary
FROM            Employee
WHERE        (Salary =
                             (SELECT        MAX(Salary)
                               FROM            Employee))

但是,每当我运行它时,我都会收到以下错误

错误来源:SQL Server Compact ADO.NET 数据提供程序
错误消息:解析查询时出错。[Token line number = 1, Token line offset = 69, Token in error = SELECT]

我想知道这是否是我不理解子查询是如何工作的,尽管我的导师向我保证我试图运行的语句是正确的,任何人都可以对这种情况有所了解,谢谢。

4

2 回答 2

3

这应该适用于 SQL Server:

SELECT Name, Position, Salary
FROM Employee E
   JOIN ( SELECT Max(Salary) as Sal FROM Employee ) M  ON E.Salary = M.Sal

这是SQL 小提琴

- 编辑

听起来您正在使用不完全支持嵌套子查询的 SQL Server CE。尝试这样的事情,而不是使用IN关键字:

SELECT e.Name, e.Position, e.Salary
FROM Employee e
WHERE e.Salary IN (SELECT MAX(Salary) as sal 
                   FROM Employee)
于 2013-01-15T17:27:02.523 回答
1

精简版显然有几个限制。你可以试试这个:

SELECT e.Name, e.Position, e.Salary
FROM Employee e
  LEFT JOIN Employee s
    ON s.Salary > e.Salary
WHERE s.Salary IS NULL ;

还有这个:

SELECT e.Name, e.Position, e.Salary
FROM Employee e
WHERE NOT EXISTS
      ( SELECT *
        FROM Employee s
        WHERE s.Salary > e.Salary
      ) ;

还有这个:

SELECT e.Name, e.Position, e.Salary
FROM Employee e
WHERE e.Salary IN 
      ( SELECT MAX(s.Salary)
        FROM Employee s
      ) ;

和这个(也许CE不允许GROUP BY在子查询/派生表中,或者在整个表上分组):

SELECT e.Name, e.Position, e.Salary
FROM Employee e
WHERE e.Salary = 
      ( SELECT TOP (1) s.Salary
        FROM Employee s
        ORDER BY s.Salary DESC
      ) ;
于 2013-01-15T17:44:09.973 回答