Q
Problem solve Get help with specific problems with your technologies, process and projects.

Latest two rows for each group

I have two tables, one for storing project information and the other for project updates.

Table1
ProjID (key)
Proj_Title
Proj_Description

Table2
PProg_ProjID (foreign key to Table1 ProjID)
PProgID (key)
PProg_updates
PProg_datetime

What I wish to retrieve is the last two progress updates (based on PProg_datetime) from Table2, for each ProjID in Table1.


This question is similar to Latest row for each group which discussed two different queries to produce the same desired result. Let's adapt those two queries to this question, but get the latest two rows for each group.

Here's the first query. The table names have been changed to match this particular question.

select Proj_Title
     , Proj_Description
     , PProg_updates
     , PProg_datetime
  from Table1
inner
  join Table2 T
    on ProjID = PProg_ProjID
 where PProg_datetime
     = ( select max(PProg_datetime)
           from Table2
          where PProg_ProjID = T.PProg_ProjID
       )
    or PProg_datetime
     = ( select max(PProg_datetime)
           from Table2
          where PProg_ProjID = T.PProg_ProjID
            and PProg_datetime 
              < ( select max(PProg_datetime)
                    from Table2
                   where PProg_ProjID = T.PProg_ProjID
                )
       )

The second query, involving a self-join, has to be extended to a third table. Note that a LEFT OUTER join is required, in case there is only one latest row in each group, not two:

select Proj_Title
     , Proj_Description
     , T1.PProg_updates
     , T1.PProg_datetime
  from Table1
inner
  join Table2 T1
    on ProjID = PProg_ProjID
inner
  join Table2 T2
    on T1.PProg_ProjID = T2.PProg_ProjID
left outer
  join Table2 T3
    on T2.PProg_ProjID = T3.PProg_ProjID
   and T2.PProg_datetime > T3.PProg_datetime 
group
    by Proj_Title
     , Proj_Description
     , T1.PProg_updates
     , T1.PProg_datetime
having T1.PProg_datetime = max(T2.PProg_datetime)
    or T1.PProg_datetime = max(T3.PProg_datetime)

In Microsoft SQL Server and Access, there is yet another solution, using the TOP keyword:

select Proj_Title
     , Proj_Description
     , PProg_updates
     , PProg_datetime
  from Table1
inner
  join Table2 T
    on ProjID = PProg_ProjID
 where PProg_datetime
    in ( select top 2 
                PProg_datetime
           from Table2
          where PProg_ProjID = T.PProg_ProjID
         order 
             by PProg_datetime descending
       )

Much neater.


This was last published in December 2003

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close