I want to select the most expensive product each customer bought, but I have the information in 3 tables: Customers, Purchases, ProductsPrices.
Tables look like:
Customers:
Customer_ID | Customer_Name
Purchases:
Customer_ID | Product_ID
ProductPrices:
Product_ID | Price
What i'm running is:
SELECT
Customer_Name, max(Price), Purchases.Product_ID
FROM Customers
LEFT JOIN Purchases
ON Customers.Customer_ID=Purchases.Customer_ID
LEFT JOIN ProductPrices
ON Purchases.Product_ID=ProductPrices.Product_ID
GROUP BY Customer_Name
ORDER BY ABS(Price) DESC
;
And the output i'm getting is the names and the highest purchase correct, but the product_id is the first, and not associated with the highest price.
Can you help me to spot what am I doing wrong?
EDIT:
To make it easier for you, I created this: