MS SQL MAX QUERY, how do I return "multiple" MAX values in one query?

Posted on 2013-06-18
Last Modified: 2013-06-18
I have two tables.  One contains a big pile of product information as well as the page number that the item appears in, in a catalog.  The second table contains the imagePath for the items, but each item has two images associated with it.  One is low res, the other high res.  Using the supplier code, how do I return only the high res imagePaths for each code?  I feel that MAX is the right path to proceed down, but I'm probably wrong.

SELECT MAX(tblImagePaths.fldItemSize) AS LargeImage, tblSupplierData.fldPage, tblImagePaths.fldItemCode
FROM tblSupplierData
INNER JOIN tblImagePaths ON tblSupplierData.fldItemCode = tblImagePaths.fldItemCode
WHERE (tblImagePaths.fldItemCode, 5) = @monthYear) AND (tblSupplierData.fldPage = @pageNumber)
GROUP BY tblImagePaths.fldItemSize, tblSupplierData.fldPage, tblImagePaths.fldItemCode
Question by:StuBabyAight
  • 2

Author Comment

ID: 39258395
Just to clarify my question.  If the user selects a page number from a particular catalog, then I'm trying to return ALL items on that page and their respective highest resolution images.  So the result set would be something like
Page 5, Item 65, Imagepath c:\etc, etc
Page 5, Item 66, Imagepath c:\etc, etc
Page 5, Item 67, IamgePath c:\etc, etc
LVL 24

Accepted Solution

chaau earned 500 total points
ID: 39258425
Use CTE to prepare the statement for the hi-res images:

;with CTE as
SELECT tblImagePaths.fldItemCode,
tblImagePaths.fldImagePath,   -- <-- Please put here the proper column name for the Image Path
ROW_NUMBER OVER(PARTITION BY tblImagePaths.fldItemCode ORDER BY tblImagePaths.fldItemSize) row_num
FROM tblImagePaths 
SELECT tblSupplierData.fldPage, tblImagePaths.fldItemCode, 
CTE.fldImagePath   -- <-- Please put here the proper column name for the Image Path
FROM tblSupplierData
INNER JOIN CTE ON tblSupplierData.fldItemCode = CTE.fldItemCode AND CTE.row_num = 1
WHERE (tblSupplierData.fldItemCode, 5) = @monthYear) AND (tblSupplierData.fldPage = @pageNumber)

Open in new window

I have not tested it, but pretty sure it should work once you put the proper column name for Image Path

Author Closing Comment

ID: 39258437
Awesome, thanks very much!

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question