Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Latest row for each group
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Latest row for each group

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 17 December 2003
I have a table with two fields, RequestID and RequestDate. I want a query to return the latest distinct RequestIDs. In the sense, if there are four records of RequestID 1 and three records of RequestID 2, I would like to get back only two records, which have the latest RequestID 1 and the latest RequestID 2.

>

The standard SQL approach to this problem is really quite straightforward:

select RequestID 
     , RequestDate
  from yourtable T
 where RequestDate
     = ( select max(RequestDate)
           from yourtable
          where RequestID = T.RequestID
       )

This query uses a correlated subquery. Note the use of the T alias to correlate the rows of the subquery to the row of the outer query. In effect, correlation like this is another way to do grouping.

However, if you are using MySQL, versions prior to 4.1, the above will not work. In that case, try this:

select T1.RequestID 
     , T1.RequestDate
  from yourtable T1
inner
  join yourtable T2
    on T1.RequestID = T2.RequestID
group
    by T1.RequestID 
     , T1.RequestDate
having T1.RequestDate = max(T2.RequestDate)

This query uses a self join, joining the table to itself on matching keys, and an explicit GROUP BY clause to perform the same grouping as the first query, with a HAVING clause to filter the rows of the group. Not surprisingly, it produces the same result set. (As the saying goes, "Your mileage may vary; mine doesn't.")

This query works in other databases, too, not just MySQL. I cannot comment on which query is more efficient, though, since each database's optimizer is different; however, I expect a good optimizer to produce efficient, if not identical, execution plans for both queries.

See also Latest two rows for each group.


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



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



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

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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