Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Select

Status
Not open for further replies.

pwright

Technical User
Feb 13, 2002
3
0
0
US
Has anyone written sql to select exactly 10% of the records from an existing file. The records must be randomly selected from throughout the file.
 
Well, it won't get you EXACTLY 10%, but it comes close:
Code:
SELECT * FROM myTable WHERE RandTest() INTO CURSOR myResult

FUNCTION RandTest()
    IF RAND()<.9
        RETURN .F.
    ENDIF
ENDFUNC
Ian
 
If you don't have to use SQL, you could do something
like this ... and this will give you exactly 10%.

Assuming you have 1000 records in the file from which you are selecting records ...

nPicked = 0
do while nPicked < 100
nRecno = round(1000 * rand(),0) && Pick a random recno
select file_to_pick_from
go nRecno
if empty(picked_field)
replace picked_field with &quot;X&quot;
scatter memvar
select file_to_put_records_into
append blank
gather memvar
nPicked = nPicked + 1
endif
enddo

Just another approach ...
Good Luck
Don
dond@csrinc.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top