Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Learned something today? Share it, or learn from what others have learned today

Get random row from the table

Feb 21 2012 12:00AM by Adam Tokarski   

Sometimes it happens that we need random row (or set of randoms rows) from table. Itzik Ben-Gan shows us, in one of his book, the efficient way to do this:

SELECT TOP(1) * FROM someTable ORDER BY NEWID()

Changing the value in TOP operator, we can, in a simple way, get more random rows.

Read More..  [4892 clicks]


Adam Tokarski
75 · 3% · 691
17
 
6
 
10
 
0
Incorrect
 
0
Interesting
 
 
0
Move



Submit

12  Comments  

  • Superb! And so simple!

    commented on Feb 21 2012 1:38AM  .  Report Abuse This post is not formatted correctly
    dishdy
    13 · 13% · 2916
  • This technique is really awesome, I have been using this in my Promotion project to get winners randomly from data from last couple of years.

    commented on Feb 21 2012 4:36AM  .  Report Abuse This post is not formatted correctly
    Mushtaq Ilyas
    1092 · 0% · 12
  • Hmmhh, I was about to be clever and generate some random data for a TSQL Challenge only to find that this NEWID() critter is truly random. It's not like RAND() where you can initially invoke RAND(1) and then get the same random numbers in subsequent invocations of RAND(). I guess NEWID() is meant to be truly random and non repetitive - no matter what your needs :-)

    commented on Feb 21 2012 8:13AM  .  Report Abuse This post is not formatted correctly
    dishdy
    13 · 13% · 2916
  • nice tip! Although the sql needs to be

    SELECT TOP(1) * FROM sometable ORDER BY NEWID() or

    SELECT TOP(1) columnlist FROM sometable ORDER BY NEWID()

    commented on Feb 21 2012 8:35AM  .  Report Abuse This post is not formatted correctly
    vanne040
    98 · 2% · 457
  • vanne040 of course, you're right. Already edited.

    commented on Feb 21 2012 8:47AM  .  Report Abuse This post is not formatted correctly
    Adam Tokarski
    75 · 3% · 691
  • NEWID() is evaluated for each resulting rows while RAND() is evaluated for the whole resultset.

    It's really important to know NEWID is evaluated afterward in order to understand what happens when you try this :

    SELECT
        *
    FROM (VALUES(1), (2)) AS X(x)
    CROSS JOIN (VALUES (NEWID())) AS Y(y)
    
    commented on Feb 21 2012 8:53AM  .  Report Abuse This post is not formatted correctly
    Sergejack
    42 · 5% · 1110
  • Sergejack, ...RAND() is evaluated for the whole resultset... Yes that's the key to this issue.

    commented on Feb 21 2012 9:10AM  .  Report Abuse This post is not formatted correctly
    dishdy
    13 · 13% · 2916
  • Nothing is wrong. Just confirmed what you said.

    commented on Feb 21 2012 10:05AM  .  Report Abuse This post is not formatted correctly
    dishdy
    13 · 13% · 2916
  • Here's how I usually query for a random record:

    SELECT top 1 AccountNumber 
    FROM
             (SELECT  a.AccountNumber ,
                      RandomNumber = RAND(CAST(NEWID() AS BINARY(6)))
              FROM AccountTable a
              WHERE AcctStartDate >= '1/1/2012') as  vt
    ORDER BY vt.RandomNumber
    

    I'll usually plug this into a declare @variable int... set @variable = (Select.... ) for testing out a parameter on a report.

    commented on Feb 22 2012 11:01AM  .  Report Abuse This post is not formatted correctly
    Elizabeth
    1158 · 0% · 11
Previous 1 | 2 Next

Your Comment


Sign Up or Login to post a comment.

"Get random row from the table" rated 5 out of 5 by 17 readers
Get random row from the table , 5.0 out of 5 based on 17 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising