SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Site Related Forums
 Article Discussion
 Article: Multiple Random Records w/Single Select
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 10/03/2000 :  18:01:43  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
Reverend writes "This has got me. I am looking for a way to create a record set with multiple random values using a single, non-iterated/non-looped, select statement. On a 265 record DB I even tried . . .

Article Link.

Anonymous
Starting Member

0 Posts

Posted - 11/16/2000 :  12:41:21  Show Profile  Reply with Quote
Mike C

select top 10 * from myTable order by NewId()

Is a very quick and easy random select.
Change 10 to the number of records you want and myTable to your table name of course.





Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 12/01/2000 :  07:39:46  Show Profile  Reply with Quote
Random Select

I too have a requirement to produce x random records from a table with a WHERE clause set. But how exactly does this code work ?

"select top 10 * from myTable order by NewId()"

I found similar code for Access at http://www.mvps.org/access/:

"select top 100 mytable.*from mytable
where Randomizer() = 0
order by rnd(isnull(mytable.question) * 0 + 1)"

where the Randomizer function is:
Function Randomizer () As Integer
Static AlreadyDone As Integer
If AlreadyDone = False Then Randomize : AlreadyDone = True
Randomizer = 0
End Function

This appear to be a variant of the same thing to work around the lack of NewID() in Access. I'm puzzled as to how either do their stuff. It looks like a way of tapping into (or fooling) the actual SQL parsing/searching mechanism. Is this approach legal/reliable ? Any insights would be very gratefully received...

Bob Osola
bobo@tmcs.co.uk

Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 02/16/2001 :  18:34:53  Show Profile  Reply with Quote
Random Select

I've developed a Ranomizing select that will return you any number of records in an *almost* random order. You can see patterns but for the most part they are random.

Randomness and Recordsets...

Send me an email with your comments.

Ryan Trudelle-Schwarz
ryan@mamanze.com

Go to Top of Page

Channing_Jones
Starting Member

2 Posts

Posted - 12/09/2003 :  12:00:09  Show Profile  Reply with Quote
You can also use the Sine-function to generate a nearly-random sequence:

"select top 10 * from myTable order by Sin(UnitID * Rand() * 1000)"

With UnitID being a unique value for each record. The 1000 ensures that the numbers are not within the same 2pi-period.
Go to Top of Page

kamleshdorle
Starting Member

India
1 Posts

Posted - 04/23/2010 :  01:58:11  Show Profile  Reply with Quote
I have saved 1000 Question in Sql server 2005 database table And I want to retrieve the any 50 questions in random order just like the online exam pattern for perticular session.

MY name is Kamlesh
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000