Write a sub-query that returns the total number of orders placed by the customer with the highest number of orders. Include the customerid and the number of orders placed.

USE Northwind
GO
SELECT CustomerID,
       COUNT(Orders.OrderID) AS TotalOrders,
       SUM(OrderAmounts.DollarAmount) AS TotalDollarAmount
FROM [Orders]
Left Outer JOIN (SELECT OrderID, Sum(Quantity*SalePrice) AS DollarAmount 
      FROM OrderItems GROUP BY OrderID) AS OrderAmounts
  ON Orders.OrderID = OrderAmounts.OrderID
GROUP BY CustomerID
ORDER BY Count(Orders.OrderID) DESC

I get this response: Msg 208, Level 16, State 1, Line 1 Invalid object name 'OrderItems'.

???

有帮助吗?

解决方案

Is the table OrderItems a member of the Northwind database or somewhere else? You may have to fully qualify it with [DatabaseName].dbo.[OrderItems]

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top