* Make sample data Use in select('thedbf') Create cursor thedbf (cGroup C(10), dDATE D) For iigroup = 1 to 9 ciigroup = ltrim(str(iigroup)) For ii = 1 to 9 cii = '0'+ ltrim(str(ii)) Insert into thedbf (cGroup, dDATE) values (ciigroup, ctod(cii+'/'+cii+'/2001')) Endfor Endfor * Now, instead of: * Get top 3 per cGroup * Select top 3 thedbf.dDATE, thedbf.cGroup FROM thedbf order by 1 descend GROUP BY thedbf.cGroup * 1) fetch the groups available select thedbf.cGroup from thedbf group by thedbf.cGroup into cursor tmpGroups * 2) for each of these groups scan lcGroup = tmpGroups.cGroup * 3) if it is the first group create the first version of the result cursor if recno()=1 Select top 3 thedbf.dDATE, thedbf.cGroup FROM thedbf where thedbf.cGroup=lcGroup order by 1 descend into cursor tmpTop3 else * 4) if not, add the rows for this group to the previous state of the result Select top 3 thedbf.dDATE, thedbf.cGroup FROM thedbf where thedbf.cGroup=lcGroup order by 1 descend into cursor tmpAdded select * from tmpAdded ; union ; select * from tmpTop3 ; into cursor tmpNewTop * 5) step over caching issues... select * from tmpNewTop into cursor tmpTop3 endif * 6) go to next group select tmpGroups endscan * 7) the final version of the result cursor if reccount("tmpGroups")>0 select * from tmpTop3 order by 1 descend endifbye