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 2000 Forums
 Transact-SQL (2000)
 MINs and MAXs from joined tables

Author  Topic 

julianfraser
Starting Member

19 Posts

Posted - 2005-06-21 : 08:46:06
Basically I need to creat a query that will return the detaileds of the item specified by the itemID and then from the linked table containing photos of the item (indexed by photoID) I need to get one record. This record will essentially be the record linked to itemID with the lowest list order (1 first item in the list). The photos are indexed by photoID and the quety below returns only the highest photoID value which is not the lowest list_order value.

If I remove [@item_photo_order = MIN(pp.list_order), ] from the code, then put [MAX(pp.photoID)] and add [ORDER BY pp.list_order DESC] will this work or is this a bad way to go about this?

Any help would be great.

Thanks,
Julian.

SELECT @item_photo_order = MIN(pp.list_order), @item_photoID = pp.photoID, @item_photo_ext = pp.file_extension,
@item_title = p.title, @item_price = p.price, @item_type = pt.item_type
FROM item p
INNER JOIN item_type pt ON p.item_typeID = pt.item_typeID
LEFT JOIN item_photo pp ON p.itemID = pp.itemID
WHERE p.itemID = @itemID
GROUP BY pp.list_order, pp.photoID, pp.file_extension, p.title, p.price, pt.item_type


madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-06-22 : 01:36:11
Can you post table structures with sample data and expected outcome?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -