| Olaf Doschke 2005-04-26, 7:24 am |
| > Is ther a way to write a VFP query that will return 4 random records?
My first suggestion is something like that:
select * from table where id = 224 and rand()<(4/25)
But this will not select exactly 4 records each time,
as it's randomly selecting or not selecting the records
with a chance of 4 in 25.
A better way would be making a random numbering,
order by that and take the first 4 of that:
Select top 4 rand() as randomnumber,*;
from table where id = 224 order by randomnumber
This has a disadvantage of not being optimizable.
First each record has to be extended with a random
number, before the first 4 of those numbers can
be determined. But with only 25 records in each group
that should be fast enough.
You should initialize the random number generator
with once calling =rand(-1), somewhere in your main.prg
to get different results with each run.
Bye, Olaf.
|