I need to only get the line with the highest transactiontime per productId. So In this case I need to get the first line and all the other lines with productid 224 should be gon
You could do:
SELECT MT.NQ, MT.ProductId, MT.Product, MT.Warehouse, MT.ProductType, MT.Transactiontime
FROM @MaxTime MT
INNER JOIN (
SELECT ProductId
, MAX(Transactiontime) AS 'TransactionTime'
FROM @MaxTime
GROUP BY Productid
) GR
ON MT.ProductId = GR.ProductId
AND MT.TransactionTime = GR.TransactionTime
ORDER BY MT.ProductId