Results 1 to 4 of 4

Thread: simple question

  1. #1
    Join Date
    Jun 2004
    Posts
    9

    Unanswered: simple question

    want to ask a simple one want the query to pull a single rendom row from a table.... all columns thanks GRT

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    which database?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    A table is a black box.

    Close your eyes.
    Put your hand into the table.
    Draw one record out.

    I tried it once and it worked!

  4. #4
    Join Date
    Sep 2004
    Location
    Belgium
    Posts
    1,126
    Assuming there is a function RAND() which returns a random positive integer number in the range 1 to n (the table size), you could do the following, where I'm assuming the table has a (single) primary key column named PK.
    Code:
    SELECT * FROM table
    WHERE  pk = ( SELECT a.pk FROM table AS a INNER JOIN table AS b ON a.pk<=b.pk
                  GROUP BY a.pk
                  HAVING COUNT(*) = RAND()
                )
    If your RDBMS supports OLAP, this is equivalent to
    Code:
    SELECT * FROM table WHERE rank() OVER (ORDER BY pk) = RAND()
    It's fairly straightforward to construct such a function RAND() from a random number generator function that e.g. generates decimal numbers between 0 and 1.
    Last edited by Peter.Vanroose; 06-11-06 at 12:30.
    --_Peter Vanroose,
    __IBM Certified Database Administrator, DB2 9 for z/OS
    __IBM Certified Application Developer
    __ABIS Training and Consulting
    __http://www.abis.be/

Posting Permissions

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