CREATE CURSOR ttest (AreaCode I, Team I) =RAND(-1) FOR i=1 TO 1000 INSERT INTO ttest VALUES (INT(RAND()*100000), INT(RAND()*1000000)) ENDFOR lnNumberToExtract = 100 SELECT TOP lnNumberToExtract *, RECNO() AS rn, ; ROUND(RAND()*10000000000,0) AS ord ; FROM ttest ; ORDER BY ord ; INTO CURSOR crsRand browse>hi ,thank you
>CLOSE all >SET DELETED ON > >SET TALK OFF >SET ECHO OFF >*Moved next two lines here, because we need to open table2 just once, not inside the scan loop >USE ttest in 0 alias tTest select tTest >SET FILTER TO team=0 >DELETE ALL >PACK > local aa > aa= thisform.text1.value && Make sure, that textbox initial value is set to 0 > SELECT 0 && Always use 0 instead of explicit number, 0 will open the next available area > USE hato alias hato && I would use better names for my tables even for temporary tables >* Step 1 - calculate percentages >* Each team record count per each area >select areacode, team, count(*) as TeamCount from ttest ; >group by areacode, team into cursor curTeamCount > >* Total number of records per area from every team >TotalFetchNumber=m.aa >select areacode, count(*) as AreaCount from ttest ; >group by areacode into cursor curAreaCount > >#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 > >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 m.lnNumberToExtract *, RECNO() AS rn, ; && i get the error meesage . > ROUND(RAND()*10000000000,0) AS ord ; > FROM ttest ; > 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 ttest WHERE AreaCode = m.lnAreaCode and Team = m.lnTeam into cursor crsRand NOFILTER >endif > >>