Solved

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

Posted on 2013-06-18
3
469 Views
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
0
Comment
Question by:StuBabyAight
  • 2
3 Comments
 

Author Comment

by:StuBabyAight
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
0
 
LVL 24

Accepted Solution

by:
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
0
 

Author Closing Comment

by:StuBabyAight
ID: 39258437
Awesome, thanks very much!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now