I am not a SQL guy, I have used it in the past and rarely have an issue that cant be solved by google... however this time I need to ask the Community.
I have a database
You're on the right track. What if you use:
select t.ProdNo, t.TransactionDate as 'LastPurchaseDate', t.Price
from Transactions t
inner join (
select ProdNo, max(TransactionDate) as 'LastPurchaseDate'
from Transactions
WHERE Price > 0
group by ProdNo
) tm on t.ProdNo = tm.ProdNo and t.TransactionDate= tm.LastPurchaseDate
Note the change in join conditions.
What happened in your query: LastPurchaseDate = tm.LastPurchaseDate
. There is only one column called LastPurchaseDate
, so it's equating it with itself, which is always true. So you're left with t.ProdNo = tm.ProdNo
, since t.ProdNo
is not unique, you get multiple records returned for each t.ProdNo
.