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

Randomizing records retrieved from a query

Here's the basic idea of what I'm doing. I'm creating a database with a list of practice questions for a class. I've got a table with a list of questions. Each has a field QstID (primary key, numeric), QstQuestion (string, question being asked), and QstTag (string, the text tag provided by the user)... There are other fields as well, but they are irrelevant to what I want to do.

Let's say a user wants to do a quiz of all the questions they have tagged "Review." I would like to add these records with this value for QstTag to another table called TblTestBank. I know how to do this via constructing an SQL statement with INSERT INTO. Where I'm running into a problem is here:
I want to be able to add these questions to TblTestBank in a random order so that the questions are not presented the same each time the user takes the quiz. Is this something that can be achieved by adjusting the ORDER BY clause? Or is there some other mechanism of doing this that I have not considered?
Aug 6 '11 #1
4 1598
NeoPa
32,556 Expert Mod 16PB
Interesting question Jennifer.

I don't believe there is anything inbuilt in SQL that provides that functionality.

However, there are some clever little ideas out there and I think I have managed to get one of them to work (I don't know the name of your table so I've used [tblQuestion], and I've also assumed ten questions are required for the test.) :
Expand|Select|Wrap|Line Numbers
  1. SELECT   TOP 10 *
  2. FROM     [tblQuestion]
  3. ORDER BY Rnd(Timer()*[QstID])
No intermediate table is required for this.
Aug 6 '11 #2
ADezii
8,834 Expert 8TB
@JenniferM:
Here is a more complex Version of NeoPa's Logic which I use to generate Random Questions in a couple of Databases.
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim qdf1 As DAO.QueryDef
  3. Dim qdf2 As DAO.QueryDef
  4. Dim strTag As String
  5. Dim intNumOfRecords As Integer
  6. Dim intNumOfQuestions As Integer
  7.  
  8. '******************** Enter Your Own Values Here ********************
  9. strTag = "Review"
  10. Const conQUERY_NAME As String = "qryRandom"
  11. intNumOfQuestions = 10
  12. '*******************************************************************
  13.  
  14. intNumOfRecords = DCount("*", "tblQuestions", "[QstTag] = '" & strTag & "'")
  15. Select Case intNumOfRecords
  16.   Case 0
  17.     MsgBox "No Records for the [" & strTag & "] Tag!"
  18.       Exit Sub
  19.   Case Is < intNumOfQuestions
  20.     intNumOfQuestions = intNumOfRecords     'RESET
  21.       MsgBox "There are only " & intNumOfQuestions & " questions that you can test " & _
  22.              "on. The Number of Questions has been Reset"
  23.   Case Else
  24.     'We are OK, so just fall through
  25. End Select
  26.  
  27. 'If the Query already exists, then DELETE it
  28. For Each qdf1 In CurrentDb.QueryDefs
  29.   If qdf1.Name = conQUERY_NAME Then
  30.     CurrentDb.QueryDefs.Delete conQUERY_NAME
  31.       Exit For
  32.   End If
  33. Next
  34.  
  35. 'If conNUM_OF_QUESTIONS is > the actual Number of Records in the Query, all Records
  36. 'in the Query will be returned
  37. strSQL = "SELECT TOP " & intNumOfQuestions & " tblQuestions.QstID, tblQuestions.QstTag, " & _
  38.          "tblQuestions.QstQuestion FROM tblQuestions WHERE tblQuestions.QstTag = '" & strTag & "' " & _
  39.          "ORDER BY Rnd(Timer()*[QstID] );"
  40.  
  41. Set qdf2 = CurrentDb.CreateQueryDef(conQUERY_NAME, strSQL)
  42.  
  43. DoCmd.OpenQuery conQUERY_NAME, acViewNormal, acReadOnly
  44. DoCmd.Maximize
Aug 7 '11 #3
Jennifer,

Not sure which version of SQL you are using but in SQL Server 2008 there is a function NEWID. It can be used in the ORDER BY clause:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM
  3. TblTestBank 
  4. WHERE
  5. Tag = 'Review'
  6. ORDER BY NEWID()
  7.  
This returns a list in a different order each time it is run.
Oct 24 '11 #4
NeoPa
32,556 Expert Mod 16PB
This is in the Access forum so the type of SQL used would generally be Jet SQL (unless a pass-thru is used). The NEWID() facility provided by T-SQL supports something which is handled quite differently in Access (so I'm afraid isn't available). Nice thinking though :-)
Oct 24 '11 #5

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

Similar topics

3
by: Matt. | last post by:
Hi all! The SQL is below. Here is the scenario. The table Production contains all of the production information for our plant. Primary key is Job #, ProDate, Shift, Machine #. The table...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
1
by: Tom Keane | last post by:
I pretty much want to know how to get the number of records returned from a query, so I can divide that number into another number and place it as another field in the query result. Is this...
5
by: chrisc | last post by:
Hello, Hope this is the right place for this... I am creating a testing database for components as they come off a production line. My reports need to select faults that are found, as well...
7
by: John | last post by:
I currently have a form and subform based on two tables; tblGoodsIn and tblGoodsInDetail. The fields in the underlying tables do not contain any price information. only foreign key links to...
1
by: VMI | last post by:
I need to display Access data in a datagrid but the Access table has over 2 million records. Since I can't fill a datatable with all those records but the user needs to see all of them, how can I...
3
by: leo_kroon | last post by:
In een formulier gebruik ik een query van meer dan 100 records. Hiervan wil alleen de eerste 50 laten zien. Als een query een recordteller zou bevatten, zou een eenvoudig criterium zijn "teller <...
3
by: Rashapoo | last post by:
I have just completed a basic course in Access. I have some relational b/g in DB2 and Cobol. I have a question my instructor couldn't answer (or maybe I didn't explain myself) I design a...
2
by: Allen Anderson | last post by:
Hi, I'm trying to design contact (names and addresses) tables in an Access database. Some of the contacts represent vendors, some are board members of the organization, some are donors, some...
1
by: Brit | last post by:
I have an ASP file that retrieves names from an Access database for 4 different categories of membership, which the visitor to the page selects (corporate, institutional, regular, or student). The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
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: 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
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.