Solved

can SQL do an Order By RND (KEY) ?

Posted on 2013-12-08
6
363 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 83

Expert Comment

by:Dave Baldwin
ID: 39705437
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
ID: 39705537
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
ID: 39705539
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
ID: 39705545
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
ID: 39705601
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
ID: 39705800
wow. that worked for me too.   thank you
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

914 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

19 Experts available now in Live!

Get 1:1 Help Now