SQL subqueries errors

依然范特西╮ 提交于 2019-12-25 04:24:52

问题


Using the AdventureWorks2012 database, I have to change the query below to select the top 10% orders with CustomerId greater than 500 and sequence the orders by TotalDue in descending sequence.

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

Here are two of my attempts at solving the problem, but both contain errors:

  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'.

Attempt 1

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

What am I doing wrong? Thanks.


UPDATE: I removed the comma from the ORDER BY section and managed to execute the query from my first attempt. However, I get an seemingly endless "Executing Successfully" load. In other words, I'm getting over 3000 results and counting when I modified the query below:

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

来源:https://stackoverflow.com/questions/21096582/sql-subqueries-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!