Solved

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

Posted on 2013-06-18
3
467 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
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome, thanks very much!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need Counts 11 39
t-sql complement 8 29
SQL Server 2012 Row Selection 2 28
MSSQL Frequency of Years From Days Field 2 11
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

763 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

12 Experts available now in Live!

Get 1:1 Help Now