SQL子查询错误

时间:2014-01-13 16:40:40

标签: sql subquery adventureworks

使用AdventureWorks2012数据库,我必须更改下面的查询以选择CustomerId大于500的前10%订单,并按降序顺序按TotalDue排序。

 SELECT *
 FROM Sales.SalesOrderHeader h
 INNER JOIN Sales.SalesOrderDetail d 
 ON d.SalesOrderId = h.SalesOrderId

以下是我尝试解决问题的两个方法,但都包含错误:

  Msg 156, Level 15, State 1, Line 9
  Incorrect syntax near the keyword 'DESC'.
  Msg 156, Level 15, State 1, Line 12
  Incorrect syntax near the keyword 'FROM'.

尝试1

 SELECT *
 FROM SalesOrderHeader 
 WHERE SalesOrderID IN
 (SELECT TOP 10 PERCENT SalesOrderID 
 FROM SalesOrderDetail 
 WHERE SalesOrderID > 500
 ORDER BY TotalDue DESC);

我做错了什么?感谢。


更新:我从ORDER BY部分删除了逗号并设法从我的第一次尝试执行查询。但是,我得到了一个看似无穷无尽的“执行成功”负载。换句话说,当我修改下面的查询时,我得到了超过3000个结果并计数:

    SELECT *
    FROM Sales.SalesOrderHeader 
    WHERE SalesOrderID IN
(SELECT TOP 10 PERCENT SalesOrderID 
FROM Sales.SalesOrderDetail 
WHERE CustomerID > 500
ORDER BY TotalDue DESC);

0 个答案:

没有答案