使用 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);