1

这是我的查询:

select * from   
(select name,empID,salary,[deducted salary] = salary-7000 from tblEmpDetails   
order by Joined_Date) TmpTbl where [decucted salary] > 50000

它给出了错误:

The ORDER BY clause is invalid in views, inline functions, derived tables,   
subqueries, and common table expressions, unless TOP or FOR XML is also specified.

然后我将查询更改为:

select * from   
(select TOP 100 PERCENT name,empID,salary,[deducted salary] = salary-7000   
from tblEmpDetails order by Joined_Date) TmpTbl where [decucted salary] > 50000 

现在它工作正常。
我的查询是使用 TOP 100 是正确的方法还是有任何其他解决方法?

4

1 回答 1

5

您可能希望在外部查询中使用 ORDER BY,例如

select name,empID,salary,[deducted salary] from   
(select name,empID,salary,[deducted salary] = salary-7000, Joined_Date
   from tblEmpDetails   
) TmpTbl where [decucted salary] > 50000
order by Joined_Date

编辑- 是的,您需要在内部查询中包含 Joined_Date 以在外部查询中对其进行排序,并明确列出所需的 4 列而不是*.

但是您也可以在一个级别编写查询

  select name,empID,salary,[deducted salary] = salary-7000
    from tblEmpDetails
   where salary-7000 > 50000
order by Joined_Date

请注意,salary-7000虽然查询中的重复只被 SQL Server评估一次,因为它足够聪明,可以使用它两次。

于 2013-05-08T04:38:34.003 回答