I have a request like
SELECT S.product_id, S.link, C.id AS category_id FROM Products P INNER JOIN SEO S ON S.product_id = P.id AND P.product_type = 1 LEFT OUTER JOIN Categories C ON c.product_id = P.id WHERE P.active = 1
I work great for me, as long as each product is assigned to only one category. But if a product is assigned to many categories, it returns all possible combinations.
Is it possible to select only the first one, and if the product does not have any category, the link should be returned using category_id = NULL
sql sql-server greatest-n-per-group
user3733648
source share