Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...,

I have a problem with sql server join query. the situation is :
i have three tables product,ProductDescription ,and ImageContainer.ImageContainer contain more than one image of each product and primary key is ProductID and my current query is :

SQL
select pm.ProductID, pm.ProductName,pm.ProductType,pm.SalesPrice,pm.DiscountPrice,pm.SubCategoryID,
 pm.BrandName,pd.Title,ic.ImageIcon,ic.SmallImage,ic.MaxImage
 from ProductMaster pm inner join ProductDescription pd
 on pm.ProductID=pd.ProductID inner join ImageContainer ic
 on pm.ProductID=ic.ProductID


This query is right, return all values( like if imageContainer contain four type of image then it returns four row for each product), but in some condition i want only single record for
each productID

thanks in advance

Amit (AV)
Posted

1 solution

Hi
if you want show one row for one product( a product with a image) you must determine which of images is your favorite image fore select.

I think bellow code can help you:

select	Product.ProductID	As	ID, 
	Product.ProductName	As	Name,
	Product.ProductType	As	Type,
	Product.SalesPrice	As	Price,
	Product.DiscountPrice	As	Discount,
	Product.SubCategoryID	As	SubCategory,
	Product.BrandName	As	Branch,
	Descriptions.Title	As	Description,
	ISNULL((Select	Top(1) ImageIcon
			From	ImageContainer
			Where	ProductID	=	Product.ProductID	--And
				-- Your Policy for select On image
				-- Order By	YourSelectPolicyColumnName	Desc
		) , 0)		As	Icon,
	ISNULL((Select	Top(1) SmallImage
		From	ImageContainer
		Where	ProductID	=	Product.ProductID	--And
			-- Your Policy for select On image
			-- Order By	YourSelectPolicyColumnName	Desc
		) , 0)		As	Image,
	ISNULL((Select	Top(1) MaxImage
		From	ImageContainer
		Where	ProductID	=	Product.ProductID	--And
			-- Your Policy for select On image
			-- Order By	YourSelectPolicyColumnName	Desc
		) , 0)		As	MaxImage,

	from	ProductMaster		As	Product 
			Inner Join 
		ProductDescription	As	Descriptions
			On	Product.ProductID	= Descriptions.ProductID 
 
Share this answer
 
Comments
avjabalpur 18-Feb-13 4:02am    
Hi Reza,

I changed your query according to my requirement and it is working thanks for your answer

best regards,

AV
Reza Alipour Fard 18-Feb-13 4:42am    
Welcome,

If my answer it's Ok for you please select my answer as "Accept Solution" and "Vote 5".
Best Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900