From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: suvisor.root@gmail.com
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #6607: Strange select behavior
Date: Mon, 23 Apr 2012 09:53:03 -0500
Message-ID: <4F95267F020000250004723E@gw.wicourts.gov> (raw)
In-Reply-To: <E1SM2Hz-0000Ox-PP@wrigleys.postgresql.org>
References: <E1SM2Hz-0000Ox-PP@wrigleys.postgresql.org>
<suvisor.root@gmail.com> wrote:
> select * from testt where id = (random()* 100000)::integer;
>
> And sometimes it comes out something like this:
> id | val
> -------+--------
> 11894 | 15051
> 29233 | 42198
> 80725 | 90213
> 85688 | 100992
> 88017 | 108075
> (5 rows)
> Here can be 2, 3 or other rows amount in result... But must be
> only one!
No, what you have written will scan the entire table and give each
row a 1 in 100000 chance of being selected. Maybe something like
this would give you what you want:
select t.* from (select (random()* 100000)::integer) n(r)
join testt t on (t.id = n.r);
By the way, you might want to tweak that random number before
casting it to int, or you might not get *any* rows back:
test=# select ('0.0000000001'::float * 100000)::int;
int4
------
0
(1 row)
Maybe something like:
(select floor(random() * 100000)::int + 1)
-Kevin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: Kevin.Grittner@wicourts.gov, suvisor.root@gmail.com
Subject: Re: BUG #6607: Strange select behavior
In-Reply-To: <4F95267F020000250004723E@gw.wicourts.gov>
References: <E1SM2Hz-0000Ox-PP@wrigleys.postgresql.org> <4F95267F020000250004723E@gw.wicourts.gov>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox