I'm working on the northwind database for my SQL studies and I'm trying to display for each year the customer who purchased the highest amount.
It seems to me that I need to do a select within a select to get the result I'm looking work. I`m managing to get the highest amount of orders for a customer, but I can't order it or just seperate it by years.
This is what I managed to do so far:
select top 1
(count([Order Details].OrderID)) 'NumOfOrders',
Customers.CompanyName, year(OrderDate) 'OrderYear'
from
[Order Details], Orders, Customers
where
[Order Details].OrderID = Orders.OrderID
and Orders.CustomerID = Customers.CustomerID
and (year(OrderDate) = 1996 or year(OrderDate) = 1997 or year(OrderDate) = 1998)
group by
Customers.CompanyName, year(OrderDate)
order by
NumOfOrders desc