I have a table that stores customer sales date like this:
|CustomerId|SalesDate |Product|Price| |1 |2015-01-01|A | 5.00| |1 |2015-02-01|A |10.00| |1 |2015-03-01|B | 7.00| |2 |2015-01-15|A | 9.00| I'd like to select a single row for each customer who has purchased product A. If a customer has purchased product A more than once, I'd like to only see the most recent purchase.
This is the desired output:
|CustomerId|SalesDate |Product|Price| |1 |2015-02-01|A |10.00| |2 |2015-01-15|A | 9.00| I've tried different versions of a group by/having queries like this:
SELECT CustomerId, SalesDate, Product, Price FROM Sales WHERE Product = 'A' GROUP BY CustomerId HAVING MAX(SalesDate);