Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

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..   [32134 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


Adam Tokarski
58 · 3% · 1021
17
 
6
 
10
 
0
Incorrect
 
0
Interesting
 



Submit

12  Comments  

  • Superb! And so simple!

    commented on Feb 21 2012 1:38AM
    dishdy
    17 · 10% · 3262
  • 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
    Mushtaq Ilyas
    616 · 0% · 57
  • 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
    dishdy
    17 · 10% · 3262
  • 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
    vanne040
    83 · 2% · 657
  • vanne040 of course, you're right. Already edited.

    commented on Feb 21 2012 8:47AM
    Adam Tokarski
    58 · 3% · 1021
  • 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
    Sergejack
    41 · 4% · 1393
  • Sergejack, ...RAND() is evaluated for the whole resultset... Yes that's the key to this issue.

    commented on Feb 21 2012 9:10AM
    dishdy
    17 · 10% · 3262
  • Nothing is wrong. Just confirmed what you said.

    commented on Feb 21 2012 10:05AM
    dishdy
    17 · 10% · 3262
  • 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
    Elizabeth
    1493 · 0% · 13
  • Why not plain NEWID()? Why are you enclosing it in RAND()?

    commented on Feb 22 2012 12:24PM
    dishdy
    17 · 10% · 3262
  • Datta, I bet it is, but I didn't manage to find it before, so I just wanted to share!

    dishdy, I believe that in Elizabeths query was, originally, just RAND, and - maybe - NEWID() was added in next steps.

    commented on Feb 22 2012 4:52PM
    Adam Tokarski
    58 · 3% · 1021

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 © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]