Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2003
    Posts
    44

    Unanswered: Selecting 10 random records

    How would I create a statement that would select 10 random records from the SQL DB?

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    USE Northwind
    GO

    SELECT TOP 10 * FROM Orders ORDER BY NewID()
    GO
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Dec 2003
    Posts
    44
    Originally posted by Brett Kaiser
    USE Northwind
    GO

    SELECT TOP 10 * FROM Orders ORDER BY NewID()
    GO
    Maybe explain what that does exactly.

    And I wont be using "GO" because I will execute the script using VBScript, this is a web app.

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    The GO is superfluous.

    NewID generates a random GUID (Globally Unique Identifer). These are guaranteed to be unique. So Brett's statement generates a random ID for each record, sorts the dataset by this new value, and then outputs the top 10.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Dec 2003
    Posts
    44
    Originally posted by blindman
    The GO is superfluous.

    NewID generates a random GUID (Globally Unique Identifer). These are guaranteed to be unique. So Brett's statement generates a random ID for each record, sorts the dataset by this new value, and then outputs the top 10.
    lol, I had no idea what NorthWind was, he should of SAID place YOUR DB there, cause I used NorthWind, heh, dumb me, but it works great.

    Thx.

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    I create my example in Query Analyzer...do you use that?
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •