473,394 Members | 1,902 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,394 software developers and data experts.

How to fetch records randomly every time you query

Hi ,
I constructed an MS access query in java to retrieve the rows in random.

The code snipet

Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement()
strQry = "select top 10 S_No , Rnd(S_No) as exp from Questions order by Rnd(S_No) desc" ;
s.execute(strQry) ;


This is the query I used to fetch the records in random. When i tried executing this code several times it fetches only the same set of records each time.

Actually my requirement is , for every time you query this , it should fetch different records.


Please help me in this.

Thanks in advance
Arun kumar
Apr 1 '08 #1
3 3886
Stewart Ross
2,545 Expert Mod 2GB
Hi. Rnd() is evaluated once only in your expression, so it returns the same randomised number for all rows. You will need to fund another approach. One such is to create a small array, populate it with a list of random numbers between 1 and the number of records N, sort the list in ascending order, then loop through your recordset selecting the rows that correspond with the random numbers. I am not a Java programmer so am unable to advise further on the code you would need.

-Stewart
Apr 1 '08 #2
Hi. Rnd() is evaluated once only in your expression, so it returns the same randomised number for all rows. You will need to fund another approach. One such is to create a small array, populate it with a list of random numbers between 1 and the number of records N, sort the list in ascending order, then loop through your recordset selecting the rows that correspond with the random numbers. I am not a Java programmer so am unable to advise further on the code you would need.

-Stewart

Hi Stewart ,

Thanks for finding your time in replying me. I understood your point well.

Let me explain you my requirement in brief.

I am in a process of creating a online test portal.
There is a table called "Questions" which has 4000 rows in it. When a user takes test , some 30 questions has to be picked up randomly and displayed to the user.

When a second user takes test another set of random questions and displayed to him. There can be repeated questions in both the occasions , but should not be the identical as the previous one.


I can randomly pick questions using java by getting all the 4000 rows and randomising using java. But my worry is the performance .

Is there any easy way to achieve this in access query?

Thanks
Arun kumar
Apr 1 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi Arun. I am sorry to say there is no easy way to do as you ask in an Access query. Access queries are based on an implementation of SQL, which in turn is based on set arithmetic. As a consequence of its set-based nature SQL has no direct concept of record number or position.

The random selection of records relies on record numbering, either express or implied, along with the generation of a set of random numbers. In SQL there is no native function which will number the rows of a query sequentially, hence why loop processing in code is necessary (making use of implied record numbering by counting the records within the loop).

There are other approaches that do not involve any programming. One I use myself to select randomised samples of student data is to export the main query details to Excel (about 6000 rows), number the rows of the table, generate a set of random numbers in another Excel sheet, and use the VLookup function on the numbered table to return the details from the randomised rows.

-Stewart
Apr 1 '08 #4

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

Similar topics

3
by: John Pastrovick | last post by:
I use a function, myrandomPIN (), to generate random PIN numbers. The following sql query updates records with the SAME PIN number but. I want to generate DIFFERENT pin numbers for every record....
3
by: Sidney Linkers | last post by:
Hi, I'm trying to make a calculated text field in a query where the textvalue is being populated from multiple records. I already use a VBA function to loop through records and concatenate the...
4
by: Mark Reed | last post by:
Hi all, I have a query (query1) which shows scan date, scan time & operator. One scan = 1 record. What I want to do is create a report based on query 2 from query1 which shows all the scans AND...
1
by: Manton | last post by:
Access 2000. I have a field in a table (tbl_data entry) which has a field called Free and another field called SD (containing 2 options: 5 and 6) The Free field is a yes/no format. I'd like to...
22
by: Nhmiller | last post by:
Is there a way to do this? Thanks. Neil Cat Paintings At Carol Wilson Gallery http://www.carolwilsongallery.com
7
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
3
by: John Fairhurst | last post by:
Hi, The following code should select the specified number of records randomly from the database <% .... query = "SELECT FROM " Set RS = Server.CreateObject("ADODB.Recordset")
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
0
by: padmaneha | last post by:
Hi Thanks for your response but the query which you gave in my previous post doesnt work. It displays the same cnt for movie reviews and videoreviews Let me explain you in detail As I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.