Solved

can SQL do an Order By RND (KEY) ?

Posted on 2013-12-08
6
361 Views
Last Modified: 2013-12-09
I want to order my select as random, but i want the same random each time.  for example, i'd like to do an Order by Rnd(KEY) so that if i change the KEY i get a new random result, but if the key is the same, the result is the same.  Possible?
0
Comment
Question by:arthurh88
6 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
No, that won't work.  The ORDER BY needs to be a field in the SELECT-ed output.  

http://msdn.microsoft.com/en-us/library/e9zc0283%28v=vs.80%29.aspx

You might be able to do

SELECT RAND(1000) AS randomnumber....  ORDER BY randomnumber

but I haven't tried that.  Calling it that way may just reseed it each time and not provide a random number.

http://technet.microsoft.com/en-us/library/ms177610.aspx
0
 
LVL 11

Expert Comment

by:Angelp1ay
Comment Utility
SELECT * FROM table
ORDER BY NEWID()

Open in new window


Edit: Ahh sorry I posted too quickly - didn't see the bit about the seed.
0
 
LVL 9

Expert Comment

by:QuinnDex
Comment Utility
not possible,  you can constrict the rand() to give you numbers in the same range but they wont come out in the same order each time the are run, so your orderby will change each time its run


if its constricting the order by to a range that you want then this should do it.
--1 to 6

ABS(Checksum(NewID()) % 5) + 1

-- 20 to 40

ABS(Checksum(NewID()) % 40) + 20

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
Comment Utility
This should work :) Tested locally and it works for me.

DECLARE @seed as int = 10;

SELECT *, r = HASHBYTES('md5', CAST(PrimaryKeyCol + @seed as varchar))
FROM YourTable
ORDER BY r

Open in new window


I have an integer primary key but I think as long as you can consistently produce as varchar from a unique column in your table plus the seed (which in this case is more like a salt) then it should work in all cases.
0
 
LVL 26

Expert Comment

by:Chris Luttrell
Comment Utility
No, the value for each occurrence of RAND() or RAND(x) for any seed value of x in a select statement gives the same value for every row in the same statement.
You can get a random ordering by " ORDER BY NEWID()" but it cannot take a seed so it will always be different, sorry.
0
 

Author Closing Comment

by:arthurh88
Comment Utility
wow. that worked for me too.   thank you
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now