SQL is a language which implements manipulation of relational database tables. These tables are based on set arithmetic, where there is no intrinsic concept of record position. It is not possible to select the Nth record in a sequence in SQL - and if it was, it would be a sequential selection, not a random one.
If you want to select a random sample of your 400k records you will need to assign a random number to each using a query. You can't just use the Rnd function in the query, as it would be evaluated just the once and return the same value for every row. But with a little ingenuity you can call a function, supplying the value of one of your table fields as an argument to the function to force re-evaluation of the random value. An example of such a function is listed below.
- Public Function fGenerateRandomNumber(somefield As Variant) As Single
-
-
'This function generates a random number using the rnd function.
-
'The value of somefield passed to the function is not otherwise used -
-
'it simply forces the random number to be generated on every call to
-
'the function, whereas if Rnd is called in a query
-
'it is only evaluated once.
-
'
-
fGenerateRandomNumber = Rnd()
-
End Function
I have tested this on an ordered sequence of numbers from 1 to 4000. If we assign a random number to each row the rows can then be ordered by that value and a selection made from the whole set. In your case, with 400k records, to select 1 in 80 (perhaps why you are looking for every 79th) you can select, say, the first 5000 records to be your random sample.
Note that the final value of the random number displayed will not match the ordering expected, as ordering will force reevaluation of the random numbers. It is nevertheless randomly ordered, as you will see in the sample from the 4000 test records below:
- No Random
-
3183 0.623072385787964
-
3557 0.323390901088715
-
1594 0.144493460655212
-
823 0.102511465549469
-
3491 0.401980400085449
-
2221 0.670504748821259
-
1706 0.301638245582581
-
3037 0.285618126392365
-
636 0.139037370681763
-
9 0.850202977657318
-
2981 0.839936137199402
-
1225 0.688546717166901
-
1106 0.126784801483154
-
3422 0.547361075878143
-
3107 1.72606706619263E-02
-
3255 0.600176632404327
-
540 0.311084508895874
The query used for this random sample set is very simple:
- SELECT No, fGenerateRandomNumber([No]) AS Random
-
FROM TestTable
-
ORDER BY fGenerateRandomNumber([No]);
Note also that you will get a different sample every time you run the query - as no two runs will generate the same set of random numbers.
-Stewart