Solved

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

Posted on 2013-06-18
3
473 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

740 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