Re: [SQL] More fun with random selects - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] More fun with random selects
Date
Msg-id 22060.930752931@sss.pgh.pa.us
Whole thread Raw
In response to More fun with random selects  (Darren Greer <dgreer@websightsolutions.com>)
List pgsql-sql
Darren Greer <dgreer@websightsolutions.com> writes:
> For example, lets say I want a random 125 rows from an existing table.  Is
> there an easy way I can do that where I actually am getting a decent sample
> from the table?

I don't think there's any way to do that with a simple SQL command.

It's possible to draw exactly N items from a set fully randomly if you
are willing to write a little code.  The algorithm looks like this:
itemsRemaining = size of set (# rows in table);itemsStillNeeded = N (# rows wanted, 125 in your example);foreach (item
inset){    generate random value X that is 'true' with probability        itemsStillNeeded / itemsRemaining;    if (X)
 {        emit current item as a selected item;        decrement itemsStillNeeded;        if (itemsStillNeeded == 0)
done;   }    decrement itemsRemaining;}
 

The random choice is typically done with a random number generator
that generates output G between say 0 and M; you make X 'true' if
G <= M * itemsStillNeeded / itemsRemaining.
        regards, tom lane


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: [SQL] String concat operator???
Next
From: Kyle Bateman
Date:
Subject: Re: [PORTS] Port Bug Report: parse error not detected onunterminated quote