Feb 20, 2002 #1 pwright Technical User Feb 13, 2002 3 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.
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.
Feb 20, 2002 #2 chpicker Programmer Apr 10, 2001 1,316 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 Upvote 0 Downvote
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
Feb 20, 2002 #3 csr Programmer Jul 20, 2000 507 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 "X" 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 Upvote 0 Downvote
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 "X" 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