BamaColtsFan
Registered User.
- Local time
- Today, 13:38
- Joined
- Nov 8, 2006
- Messages
- 91
I'm sure you follks get tired of hearing this type of question but I am struggling a little with a Random Record Selection query. I've read much of the information here and searched other sites but I am still having a problem with one small part of my query.
Basically, I need to select a random 10 percent of the records found in a table with about 2000 to 2500 records (the record count changes when the data is refreshed). Based on what I have learned so far, I need to use a SELECT TOP query that includes the Rnd() function. I have applied this in two different ways which I will show in a bit.
The method where I use a SELECT TOP 10 Percent statement (posted by MSAccessRookie) selects the correct number of records but it is always the same records every time. This is the query I use:
SELECT TOP 10 PERCENT *
FROM (SELECT Timecards.Random, rnd(Timecards.Random), INT(Timecards.Random*rnd(Timecards.Random))+1, Timecards.Name, Timecards.[People Group ]
FROM Timecards) AS [RandomList2]
ORDER BY 4;
The second method I've used actually produces a random list every time the query runs but requires that I specify number of records I want returned. The number of records needs to be a variable based on 10 percent of the record count in the Timecards table. I actually use two queries to get these results:
First, I select my records using this query;
SELECT TOP 231 Timecards.Random
FROM Timecards
ORDER BY (Rnd(Timecards.Random));
Next, I select the necessary data from the table using this query;
SELECT Timecards.Name, Timecards.[People Group ], Timecards.Random
FROM Timecards
WHERE (((Timecards.Random) In (SELECT RandomSelection.Random FROM RandomSelection)))
ORDER BY Timecards.Name;
Given that the second method is always a different set of records, I think I prefer it over the first but I need to know how to make the number of records variable. I've tried to replace the TOP value with a subquery but Access doesn't seem to like that very much. If I use the Top 10 Percent method, I get the same records every time.
So, with all that said, how do I make the TOP value a variable?
Basically, I need to select a random 10 percent of the records found in a table with about 2000 to 2500 records (the record count changes when the data is refreshed). Based on what I have learned so far, I need to use a SELECT TOP query that includes the Rnd() function. I have applied this in two different ways which I will show in a bit.
The method where I use a SELECT TOP 10 Percent statement (posted by MSAccessRookie) selects the correct number of records but it is always the same records every time. This is the query I use:
SELECT TOP 10 PERCENT *
FROM (SELECT Timecards.Random, rnd(Timecards.Random), INT(Timecards.Random*rnd(Timecards.Random))+1, Timecards.Name, Timecards.[People Group ]
FROM Timecards) AS [RandomList2]
ORDER BY 4;
The second method I've used actually produces a random list every time the query runs but requires that I specify number of records I want returned. The number of records needs to be a variable based on 10 percent of the record count in the Timecards table. I actually use two queries to get these results:
First, I select my records using this query;
SELECT TOP 231 Timecards.Random
FROM Timecards
ORDER BY (Rnd(Timecards.Random));
Next, I select the necessary data from the table using this query;
SELECT Timecards.Name, Timecards.[People Group ], Timecards.Random
FROM Timecards
WHERE (((Timecards.Random) In (SELECT RandomSelection.Random FROM RandomSelection)))
ORDER BY Timecards.Name;
Given that the second method is always a different set of records, I think I prefer it over the first but I need to know how to make the number of records variable. I've tried to replace the TOP value with a subquery but Access doesn't seem to like that very much. If I use the Top 10 Percent method, I get the same records every time.
So, with all that said, how do I make the TOP value a variable?