I'm using SQL Server 2005, and I want to query for the vendors generating the most revenue, sorted by the vendor's name. Below is the query I have tried. The inner subquery gets the 15 largest vendors sorted by revenue, and I try to order those results by the vendor name.
SELECT Revenue, VendorName
FROM (
SELECT TOP 15
SUM(po.POTotal) AS Revenue
, Vendors.VendorName AS VendorName
FROM PurchaseOrders po
INNER JOIN Vendors ON po.Vendor_ID = Vendors.Vendor_ID
WHERE ...
GROUP BY Vendors.VendorName
ORDER BY Revenue DESC
)
ORDER BY VendorName ASC
But this gives me an error message:
Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'ORDER'.
Is there another way to do this? I think this might be possible with a view, but I'd prefer not to do it that way.
I apologize if this is a duplicate, I don't even know what to search for to see if this has already been asked.