If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > simple question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-06, 15:20
GRTHIGPEN GRTHIGPEN is offline
Registered User
 
Join Date: Jun 2004
Posts: 9
simple question

want to ask a simple one want the query to pull a single rendom row from a table.... all columns thanks GRT
Reply With Quote
  #2 (permalink)  
Old 06-09-06, 19:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
which database?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-10-06, 07:25
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
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!
Reply With Quote
  #4 (permalink)  
Old 06-11-06, 11:24
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
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.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 06-11-06 at 11:30.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On