Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Only one expression can be specified in the select

Author  Topic 

R
Constraint Violating Yak Guru

328 Posts

Posted - 2008-12-15 : 13:27:45
I have a database of products, with each product having several categories that it can belong in (many-to-many relationship). A product can also have some photographs (many-to-many relationship)

I am trying to do a search to list all products, and want to include 1 photo per product in the results. When I run the following code I get an error. Could anybody recommend the correct way to achieve this result?

Please note that database admin's can set a particular product photo to be 'featured', meaning that that photo always appears for a product. This is why this clause exists in the nested select statement.

Thanks in advance.


SELECT
p.*,
s.statusDesc,
m.ManufacturerName,
c.categoryName,
(SELECT TOP 1 imageID, productID FROM tblProductImages WHERE productID = p.ID ORDER BY featured DESC, imageID DESC) AS imageID
FROM
tblProducts AS p INNER JOIN
tblCategoriesProducts cp ON p.ID = cp.ProductID INNER JOIN
tblStatus AS s ON p.statusID = s.ID LEFT OUTER JOIN
tblManufacturers AS m ON p.ManufacturerID = m.ID INNER JOIN
tblCategories AS c ON cp.CategoryID = c.ID

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 13:32:02
[code]
SELECT
p.*,
s.statusDesc,
m.ManufacturerName,
c.categoryName,
t.imageID AS imageID
FROM
tblProducts AS p INNER JOIN
tblCategoriesProducts cp ON p.ID = cp.ProductID INNER JOIN
tblStatus AS s ON p.statusID = s.ID LEFT OUTER JOIN
tblManufacturers AS m ON p.ManufacturerID = m.ID INNER JOIN
tblCategories AS c ON cp.CategoryID = c.ID
CROSS APPLY (SELECT TOP 1 imageID, productID
FROM tblProductImages
WHERE productID = p.ID
ORDER BY featured DESC, imageID DESC) t

[/code]
Go to Top of Page

R
Constraint Violating Yak Guru

328 Posts

Posted - 2008-12-15 : 14:36:19
That seems to have worked.

visakh16 you really are the "Flowing Fount of Yak Knowledge"!

Excuse me while I go read up on what CROSS APPLY actually means....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-16 : 00:10:09
quote:
Originally posted by R

That seems to have worked.

visakh16 you really are the "Flowing Fount of Yak Knowledge"!

Excuse me while I go read up on what CROSS APPLY actually means....


Thanks

http://www.sqlteam.com/article/using-cross-apply-in-sql-server-2005
Go to Top of Page

R
Constraint Violating Yak Guru

328 Posts

Posted - 2008-12-16 : 06:35:32
Hi visakh16

A small problem has appeared with your solution above. If a product has no images, then it does not appear in the search results. I think this is caused by the CROSS APPLY's select query returning NULL. Is there a way to amend the query to make it work?

Many thanks again.
Go to Top of Page

R
Constraint Violating Yak Guru

328 Posts

Posted - 2008-12-16 : 06:39:09
Okay I fixed it using OUTER APPLY.

Thank you for the link you sent, the solution to my problem was contained in it. Keep up the great work btw...!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-16 : 06:53:12
no problem...you're welcome
Sure...i will
Go to Top of Page
   

- Advertisement -