473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

select every 79th records

ddtpmyra
333 100+
I have 400K records on my Access database. Now I wanted you pull out randomly records that falls on 79th records from top to bottom. How can you do that on sql?

Your help is much appreciated.
thanks,
DM
May 15 '09 #1
7 3128
Stewart Ross
2,545 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. Public Function fGenerateRandomNumber(somefield As Variant) As Single
  2.  
  3. 'This function generates a random number using the rnd function.
  4. 'The value of somefield passed to the function is not otherwise used -
  5. 'it simply forces the random number to be generated on every call to
  6. 'the function, whereas if Rnd is called in a query
  7. 'it is only evaluated once.
  8. '
  9.     fGenerateRandomNumber = Rnd()
  10. 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:

Expand|Select|Wrap|Line Numbers
  1. No    Random
  2. 3183    0.623072385787964
  3. 3557    0.323390901088715
  4. 1594    0.144493460655212
  5. 823    0.102511465549469
  6. 3491    0.401980400085449
  7. 2221    0.670504748821259
  8. 1706    0.301638245582581
  9. 3037    0.285618126392365
  10. 636    0.139037370681763
  11. 9    0.850202977657318
  12. 2981    0.839936137199402
  13. 1225    0.688546717166901
  14. 1106    0.126784801483154
  15. 3422    0.547361075878143
  16. 3107    1.72606706619263E-02
  17. 3255    0.600176632404327
  18. 540    0.311084508895874
The query used for this random sample set is very simple:

Expand|Select|Wrap|Line Numbers
  1. SELECT No, fGenerateRandomNumber([No]) AS Random
  2. FROM TestTable
  3. 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
May 16 '09 #2
Stewart Ross
2,545 Expert Mod 2GB
Oh, I forgot to mention two things:

Firstly, the function should be placed in any public code module (one that is visible from the Modules tab in Access), or in a new module if one does not already exist.

Secondly, if you really do need to implement some form of 'select the Nth record' routine and not a random sample this can be done in VBA code using a recordset and processing through the records under program control (for example, to place the primary key values of the selected records in another table for later joining back as the selection set). However, I would advise you again that repetitively selecting the Nth record in some sequence is NOT random sampling at all.

-Stewart
May 16 '09 #3
ADezii
8,834 Expert 8TB
A lot of what I am referring to has already been stated by Stewart, so I'll not state it again. There is a little known Method of retrieving a Random Set of Rows from a Table or Query by using the previously mentioned Public Function approach and the Query's Top Value Property. If you are interested, just let me know, and I'll demonstrate how it is done.

The concept itself is rather quite simple. You add a new Column to the Query, fill it with a List of Random Numbers, and retrieve the Top N Rows, where N is a Number between 1 and the Number of Rows in the underlying data. By specifying the Top N Values for the Query, you are forcing Access to return that many Rows as the result set to the Query. You can also return a certain Percentage of the Total Rows by setting the Top Value to a Percentage (Random Sample of 37% of the Total Number of Records).
May 16 '09 #4
ddtpmyra
333 100+
Adenzii,

Please show me how to put it all together.

thanks
May 18 '09 #5
ADezii
8,834 Expert 8TB
@ddtpmyra
This Demo will display 5 Random Employees, and all their related Fields, from the Employees Table of the Northwind Sample Database:
  1. Open the Northwind Sample Database or parallel this Steps in your own Database.
  2. Create the following 'Public' Function fGenerateRandom() and pass to it any Field as long as it is contained within the Employees Table. Stewart has already given an excellent explanation as to why this is necessary in Post #2.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fGenerateRandom(varField As Variant)
    2.   Randomize
    3.   fGenerateRandom = Rnd()
    4. End Function
  3. Create the following Query by Copying-N-Pasting the SQL Statement posted below into the View ==> SQL View Pane of the New Query (Design Mode).
    Expand|Select|Wrap|Line Numbers
    1. SELECT TOP 5 Employees.*
    2. FROM Employees
    3. ORDER BY fGenerateRandom([LastName]);
  4. Switch to Design View to see the Graphical representation of the Query in the Query Grid.
  5. Look at the Top Values Property of the Query to see the relevance of the 5.
  6. Every time you execute this Query, you will see a Random Selection of 5 Employees along with accompanying Fields. The Random Number Field is intentionally Hidden since it serves no purpose displaying it.
  7. Any questions feel free to ask, and one of us will get back to you with an answer.
May 18 '09 #6
ddtpmyra
333 100+
It works! Thanks for showing it to me Adezii your help is much apreciated and thanks to Stewart for the explaining how it process.

DM
May 19 '09 #7
ADezii
8,834 Expert 8TB
@ddtpmyra
Glad the old Stew/ADezii combination worked for you (LOL)!
May 19 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
3
by: Wim Roffil | last post by:
Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
7
by: ja | last post by:
Hello, I have a table that contains approx 2 million records. I want to query 2,000 random records. Example: 500,000 names divided by 20,000 samples = 25. The N is 25, meaning samples would...
4
by: ja | last post by:
Hello all, How can I ramdomly select a list of records from an existing table? For example: 500,000 names divided by 20,000 samples = 25. The N is 25, meaning samples would be sent to every...
4
by: Jean-Claude | last post by:
Hi, which is the faster query ? (of course, in my case the real queries are more complex) 1/ select * from file1 a join file2 b on b.key=a.key where b.data=123 and b.name='TEST'
2
by: chrisale | last post by:
Hi All, I've been racking my brain trying to figure out some sort of Sub-Select mySQL statement that will create a result with multiple rows of averaged values over a years time. What I have...
8
ddtpmyra
by: ddtpmyra | last post by:
I have 400K records on my table. Now I wanted you pull out random records that falls every 79th records from top to bottom. How can you do that on sql? Your help is much appreciated. thanks, DM
45
by: dizzydangler | last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become.... I'm...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.