Re: [SQL] More fun with random selects

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: dgreer(at)websightsolutions(dot)com
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] More fun with random selects
Date: 1999-06-30 14:28:51
Message-ID: 22060.930752931@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Darren Greer <dgreer(at)websightsolutions(dot)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 in set)
{
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

Browse pgsql-sql by date

  From Date Subject
Next Message Kyle Bateman 1999-06-30 14:36:55 Re: [PORTS] Port Bug Report: parse error not detected onunterminated quote
Previous Message Herouth Maoz 1999-06-30 14:19:28 Re: [PORTS] Port Bug Report: parse error not detected on unterminated quote