Solved

can SQL do an Order By RND (KEY) ?

Posted on 2013-12-08
6
364 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
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.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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