>SET TALK OFF >SET ECHO OFFMoved next two lines here, because we need to open table2 just once, not inside the scan loop
SELECT 0 && Always use 0 instead of explicit number, 0 will open the next available area > USE table2 alias table2 && I would use better names for my tables even for temporary >* Step 1 - calculate percentages >* Each team record count per each area >select areacode, team, count(*) as TeamCount from table1 ; >group by areacode, team into cursor curTeamCount > >* Total number of records per area from every team >select areacode, count(*) as AreaCount from table1 ; >group by areacode into cursor curAreaCount > >>#define TotalFetchNumber 150 && We need to have 150 records per each area >#define Multiplier 4 >* Percentage of each team record count per area >select curTeamCount.areacode, team, TeamCount, ; > AreaCount, ; > TeamCount/AreaCount*100 as Percentage, ; > floor((TeamCount/AreaCount) * TotalFetchNumber * Multiplier) as NumToFetch ; > from curTeamCount inner join curAreaCount on ; > curTeamCount.areaCode = curAreaCount.areaCode into cursor curPercentage ; > order by 1, 2 && AreaCode, Team > >>* Close cursors, which are no longer needed >use in curAreaCount >use in curTeamCount > >>local lnNumberToExtract, lnAreaCode, lnTeam && Assuming AreaCode and Team are integer PKs > >select curPercentage >scan > > > lnAreaCode = curPercentage.AreaCode >lnTeam = curPercentage.Team > >lnNumberToExtract = curPercentage.NumToFetch >if m.lnNumberToExtract<curPercentage.TeamCount > ** Now use code from Sergey Berezniker / Al Doman > SELECT TOP lnNumberToExtract *, RECNO() AS rn, ; > ROUND(RAND()*10000000000,0) AS ord ; > FROM table1 ; > WHERE AreaCode = m.lnAreaCode and Team = m.lnTeam ; > ORDER BY ord ; > INTO CURSOR crsRand >else && we exceed original number of records, just select all records for that team > select *, recno() as rn from table1 WHERE AreaCode = m.lnAreaCode and Team = m.lnTeam into cursor crsRand NOFILTER >endif > >> > select table2 && Note, that we need to open table2 only once, not in the scan > append from dbf("crsRand") > delete from table1 where recno() in (select rn from crsRand)> >> use in crsRand && we do not need our crsRand anymore > endscan >REPORT FORM rpt1 preview >use in curPercentage > >>