?
Solved

can SQL do an Order By RND (KEY) ?

Posted on 2013-12-08
6
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 11

Accepted Solution

by:
Angelp1ay earned 2000 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 27

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

765 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