Re: selecting random row

From: Holger Klawitter <lists(at)klawitter(dot)de>
To: "pgsql-general(at)postgreSQL(dot)org" <pgsql-general(at)postgreSQL(dot)org>
Subject: Re: selecting random row
Date: 2002-04-02 08:12:02
Message-ID: 3CA967D2.12D0D8DE@klawitter.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

> Right now (in perl), I select the complete list of id's into a @list, do
> a $id = $list[rand(@list)] and have the right row. But this seems to be
> very time-consuming, and I would like to have it done completely in the
> Pg-database.

You can order by random and get the first row:

select
id
from
table
where
agr>=60 and sex = 'm'
order by
random()
limit
1
;

This will also compute the whole result set - well, you have to in order
to get the value set to select from - but at least you don't have to
pull all data into your application tier.

With kind regards / Mit freundlichem Gruß
Holger Klawitter
--
Holger Klawitter
holger(at)klawitter(dot)de http://www.klawitter.de

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mark kirkwood 2002-04-02 10:01:09 Re: Re : Solaris Performance - Profiling
Previous Message Daniel Lundin 2002-04-02 07:50:31 Re: selecting random row