Well, I am struggling with this question in SQL using MySql:
I have to give the product that was mostly sold per supplier from the popular open source database called NORTHWIND: https://northwinddatabase.codeplex.com
Now what I wrote is:
SELECT products.SupplierID ,`order details`.ProductID, count(*) as NumSales FROM `order details`
JOIN products ON `order details`.ProductID = products.ProductID
JOIN orders ON `order details`.OrderID = orders.OrderID
WHERE `order details`.OrderID
IN
(SELECT OrderID FROM orders
WHERE MONTH(OrderDate) = 7 AND YEAR(orderDate) = 1997)
group by products.SupplierID , `order details`.ProductID
ORDER BY NumSales desc
;
The result is:
that this is all good but I need to give back for example for Supplier 1 Product 1 since it was sold 3 times (at 7/1997)
Adding to the start:
SELECT SupplierID, ProductID, MAX(b.NumSales)
FROM( ... )
gets me closer but it gives my the highest of all suppliers and not for every supplier.
Help will be great.
P.S. This question is similar but the same and didn't completely help me.