Subject | Re: 4 most recent detail rows per master row |
---|---|
Author | Milan Babuskov |
Post date | 2007-09-21T18:56:59Z |
gorepj wrote:
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
> I have a master and detail table. The detail table contains a dateAssuming that detail and master and joined on field called "id":
> column. With a single SQL query I would like to return the four most
> recent rows in the detail table for each master row.
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