Home > Ask the Oracle Experts > SQL Questions & Answers > How to get the maximum value for a row
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

How to get the maximum value for a row

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 18 April 2001

Can you PLEASE PLEASE PLEASE tell me how to get the maximum value for a row?
e.g.:

      SHOPTABLE
Sid Sname Rating City 
S1  JOE     3   SYDNEY 
S2  MARCO   5   MELBOURNE 
S3  LI      4   SYDNEY 
S4  ANGELO  2   BRISBANE 
S5  DI      1   PERTH 
S6  PRU     5   MELBOURNE 
S7  VLAD    5   SYDNEY 
S8  DREW    4   HOBART 
S9  KIM     3   CAIRNS

How can I display the records that have THE HIGHEST RATING FOR THEIR CITIES?

I tried:

SELECT *
FROM ShopTable
WHERE ShopTable.Rating IN
(SELECT max(ShopTable.Rating)
FROM ShopTable)

But then all I get are those records with Rating being 5. THIS IS SO FRUSTRATING...!!!!!! Can you please help me? I will be so appreciative.
P.S. I AM USING ACCESS 2000


>
EXPERT RESPONSE

Yes, it's tricky. You have to use a correlated subselect.

SELECT Sid, Sname, Rating, City
FROM shoptable X
WHERE Rating =
       (SELECT MAX(Rating)
          FROM shoptable
         WHERE City=X.City)

The inner query gets the maximum rating in the same city as the row that the outer query is looking at, and the outer query accepts that row into the final result set if its rating has the same value as its city's maximum rating.

You sort of have to look at it for a while to see why it works. The WHERE clause in the inner query selects all the rows in the table that have the same city as the row that the outer query is looking at, which looks at every row in the table one at a time. It's called a correlated subselect, and X is the correlation variable or alias, because the rows looked at in the inner query are related to each row of the outer query by virtue of the cities having to be the same. You can think of it as evaluating the inner query once for each row of the outer query, although in practice the database might not execute it exactly that way (SQL is all about asking for what you want, not how to get it.)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts