Subject Re: 4 most recent detail rows per master row
Author Milan Babuskov
gorepj wrote:
> I have a master and detail table. The detail table contains a date
> column. With a single SQL query I would like to return the four most
> recent rows in the detail table for each master row.

Assuming that detail and master and joined on field called "id":

select d.*
from detail d
join master m on d.id = m.id
where 4 > (
select count(*)
from detail d2
where d2.id = m.id
and d2.date < d.date)


--
Milan Babuskov
http://www.guacosoft.com