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: Returning Rows in Random Order
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 07/29/2000 :  18:13:24  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
Raymond writes "How would you suggest returning records from a query in a random order?" This is a little trickier than you might think. And the best solution involves a cursor. Yuck!

Article Link.

Anonymous
Starting Member

0 Posts

Posted - 08/16/2000 :  15:01:12  Show Profile  Reply with Quote
Returning Rows in Random Order (a trick)

If your need of randomness is less strong than your laziness :)

You can use this :


select id, rand(@@IDLE % (@@ROWCOUNT + id)) as xx
from foo
order by xx

How it works :

First of all 'id' must be an integer.

@@IDLE :
Returns the time in milliseconds SQL Serverâ„¢ has been idle since it was last started.

Unless, your system is really stressed, you'll get a different value each time.

@@ROWCOUNT :
Returns the number of rows affected by the last statement.

As you can see, the more rows you've got the more 'RANDOMNESS' you get.

You can try several other 'system' variables.

This stuff should work but SQL team code works !

MHR. (mhrtst@free.fr)
Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 09/06/2000 :  03:49:28  Show Profile  Reply with Quote
I am sorry to disapoint you....

Your way is way.. way.. way.. to long.

Dont try this at home:

SELECT Name,(sin(Category.ID * rand())) as Rd,
ORDER BY Rd

guy peled

gpeled@shopserve.com

Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 09/06/2000 :  03:51:00  Show Profile  Reply with Quote
I am sorry to disapoint you....

Your way is way.. way.. way.. to long.

Dont try this at home:

SELECT Name,(sin(Category.ID * rand())) as Rd FROM Category
ORDER BY Rd

guy peled

gpeled@shopserve.com

Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 09/15/2000 :  04:39:46  Show Profile  Reply with Quote
Returning Rows in Random Order (SQL 2000 on Win2000)

How about this:

use pubs
go

select au_lname,newid()
from authors
order by newid()
go

I believe the Windows api which implements newid() no longer returns sequential guids in Win 2000. I've tested this using SQL 2000 on Win 2000. Doesn't work using SQL7 on NT4. Haven't tried SQL7 on Win2000.

--
Nayan Raval

Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 09/26/2000 :  06:09:02  Show Profile  Reply with Quote
newId() works fine with SQL 7 and win2000

NewId() works fine with SQL 7 and win2000, how elegant!
Thanks!

Go to Top of Page

Anonymous
Starting Member

0 Posts

Posted - 11/16/2000 :  14:42:57  Show Profile  Reply with Quote
A cheaper way of converting

I asked a question recently, so i'll answer one!

Another way to do this is to use the middle part of the random number that is returned. The following example uses about 6 digits starting from three in (exactly 6 from 3 if you use 1000 instead of 1024). Using RAND() is likely to be cheaper than using trigonometric functions.

SELECT ID, RAND(FLOOR( (RAND(ID)*1024-FLOOR(RAND(ID)*1024)) * 1048576) ) FROM TheTable ORDER BY 2

Go to Top of Page

nmg196
Yak Posting Veteran

United Kingdom
70 Posts

Posted - 04/25/2001 :  14:21:11  Show Profile  Visit nmg196's Homepage  Send nmg196 an ICQ Message  Reply with Quote
PROBLEM: Multiple recordsets

The method mentioned in the article returns multiple recordsets. Probably because of the way the cursor works.

Is there a way to prevent it returning more than one recordset so I can return only my SELECT * from tablename JOIN #temp ON.... statement?

Thanks,

Nick...

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.05 seconds. Powered By: Snitz Forums 2000