Hi Eloi,
Try something like this:
SELECT * FROM yourTbl t1 WHERE EXISTS
(SELECT * FROM (SELECT TOP 5 * FROM yourTbl t2 WHERE t2.code = t1.code
ORDER BY datefield desc) t3 WHERE t3.rowID = t1.rowID)
This sample requires that you have an autonumber column which would
uniquely identify each row. I named my autonumber column 'rowID'. Here
we use the Top clause to retrieve the top 5 rows for each code based on
the most recent dates. Example: code 'A' is listed for '1/1/07',
'1/2/07',...'1/10/07'
Select Top 5 * from yourTbl where code = 'A' Order By DateField Desc
This would return all the rows for code = 'A' for dates
1/6/07, 1/7/07, 1/8/07, 1/9/07, 1/10/07
We order the dates in Descending mode so that the first dates in the
return list are the most recent dates : 1/10/07 is more recent than
1/1/07. In the first sample above, I generalize this so that we can
retrieve the top 5 rows for each group of codes instead of just one
code. I use a subquery which I name t3, and I link the subquery to
yourTbl using the autonumber field 'rowID'
Rich
*** Sent via Developersdex
http://www.developersdex.com ***