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.
Requires Free Membership to View
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 first published in December 2003
Join the conversationComment
Share
Comments
Results
Contribute to the conversation