Question

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

???

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top