Re: random rows

From: Joel Burton <jburton(at)scw(dot)org>
To: Jie Liang <jliang(at)ipinc(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org, sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: random rows
Date: 2001-04-26 19:18:29
Message-ID: Pine.LNX.4.21.0104261514020.1809-100000@olympus.scw.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-general pgsql-sql

On Thu, 26 Apr 2001, Jie Liang wrote:

>
> How I can return random N rows from my select stmt?
> like:
> e.g. what my selectee is a set of 1000 rows, I want randomly
> pickup 100 of 1000.

Interesting problem.

You might get much better responses than this, but, two ideas
that might be workable:

* use a WHERE clause that checks random() > .88 . This should
give you, on average, about 120 rows out of 1000, and you
can add LIMIT 100 to ensure that you get only 100. But you're
still biased toward the start of the list. (Or, remove the
LIMIT 100, use > .9, but there's no guarantee you'll get 100--
you'll get more or less than that.

* have a plpgsql routine that gets 100 random records,
and copy these into a temporary table (since plpgsql can't
return a recordset.) Query against this table.

Or, when all else fails:

* do it in your front end (Python/Perl/PHP/Pwhatever).

If you get better ideas, and they aren't cc'd to the list, please do so.

HTH,
--
Joel Burton <jburton(at)scw(dot)org>
Director of Information Systems, Support Center of Washington

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Joao Pedro M. F. Monoo 2001-04-26 19:18:49 Re: random rows
Previous Message Kurt Seel 2001-04-26 19:07:48 Can't start Pgsql 7.1 on FreeBSD 4.2

Browse pgsql-general by date

  From Date Subject
Next Message Joao Pedro M. F. Monoo 2001-04-26 19:18:49 Re: random rows
Previous Message will trillich 2001-04-26 19:01:46 Re: crypt(table.field) ?

Browse pgsql-sql by date

  From Date Subject
Next Message Joao Pedro M. F. Monoo 2001-04-26 19:18:49 Re: random rows
Previous Message Jie Liang 2001-04-26 18:38:21 random rows