SELECT RowId = ROW_NUMBER() OVER(PARTITION BY m.SectionId ORDER BY m.ProbMax DESC),m.*
FROM Match m
LEFT JOIN Section s
ON s.SectionId = m.SectionId
WHERE m.IsValid = 1
AND s.IsValid = 1
SELECT *
FROM StorageOuts so1
INNER JOIN (
SELECT so.ProductId,
MAX(so.CreatedOn) AS CreatedOn
FROM StorageOuts so
GROUP BY
so.ProductId
) so2
ON so1.ProductId = so2.ProductId
AND so1.CreatedOn = so2.CreatedOn