Solved

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

Posted on 2013-06-18
3
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 25

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

739 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