You could convert it to a join
. But that would be uglier. In SQL Server, it is easier to express this using apply
:
SELECT BaseProducts.*
i.ImagePath, AS ImagePath
FROM CTEPage BaseProducts OUTER APPLY
(SELECT TOP 1 Images.[Path] as ImagePath
FROM Images i INNER JOIN
ProductsImages pi
ON i.Id = pi.ImageId
WHERE pi.BaseProductId = BaseProducts.Id
ORDER BY [ProductsImages].[Order]
) i;
Doing this as a join
would require using row_number()
to extract the "first" image path.