The query I’m trying to develop return the following:
- The total amount of units sold of each product
- The name of the product
- The
CustomerIDthat has bought the max amount of each product
This is what I have so far:
SELECT DISTINCT
Products.ProductName,
SUM([Order Details].Quantity) as cant,
Orders.CustomerID
FROM
Products
INNER JOIN [Order Details]
ON Products.ProductID = [Order Details].ProductID
INNER JOIN Orders
ON [Order Details].OrderID = Orders.OrderID
WHERE
[Order Details].Quantity =
(
SELECT
MAX([Order Details].Quantity)
FROM
[Order Details]
WHERE
[Order Details].ProductID = Products.ProductID
)
GROUP BY
Products.ProductName, Orders.CustomerID
It’s not giving me the results expected.
Any information related to the contents of the tables or anything else, just post it in a comment and I’ll answer.
Thanks in advance for the help!
Try this,
EDIT
The above will return duplicates if more than one customer has order the same product the maximum amount of times. This can be avoided by using the below, which will return a semi-colon separated list of customer IDs that have order each product the max number of times: