I wish to select all post from Table Category and the first row in Product table.
SQL goes (not working just to show you how im thinking):
SELECT Category.CategoryID, Category.Title, TOP (1) Product.ImagePath, TOP (1) Product.CategoryID
FROM Category INNER JOIN
Product ON Category.CategoryID = Product.CategoryID GROUP BY Category.CategoryID, Category.Title
Thanks for your help!
![]() |
0 |
![]() |
SELECT Category.CategoryID, Category.Title, Product.ImagePath, Product.CategoryID
FROM Category crossJOIN
(select top 1 * from Product ) P
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
It works almost perfectly...
But the thing is I want to get the top 1 Imagepath from table Products for each different category. Now it returns top 1 from products to all categories
SELECT
Category.CategoryID,Category.Title,Product.ImagePath,Product.CategoryIDFROM
Category CROSS JOIN(
SELECT TOP 1 * FROM Product )Product
![]() |
0 |
![]() |
?
![]() |
0 |
![]() |
SELECT c.CategoryID,c.Title,t1.ImagePath,t1.CategoryID
FROM Category c inner JOIN
(select categoryid,imagepath from (SELECT productid,categoryid,imagepath,row_number() over (partition by categoryid order by productid) as rn FROM Product ) t where rn = 1) t1
on c.categoryid = t1.categoryid
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
Thank you man! Works great!
![]() |
0 |
![]() |