pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tommy Gildseth <tommy.gildseth@usit.uio.no>
To: Stefan Zweig <stefanzweig1881@web.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: strange result for select * from table where id=random()*900
Date: Fri, 27 Jul 2007 09:56:20 +0200
Message-ID: <46A9A524.7000009@usit.uio.no> (raw)
In-Reply-To: <156384696@web.de>
References: <156384696@web.de>

On 07/27/2007 09:48 AM, Stefan Zweig wrote:
> hi list,
> 
> this is my query:
> 
> select 
> *
> from _g2977 
> where 
> id=floor(random() * (900));
> 
> in table _g2977 i have id(s) from 1 up to 900. i just want to select one random dataset by the above query but i does not work.
> 
> actually i get sometime zero, sometimes one, sometimes two and sometimes even three results back from the above query although i thought it should give only one random dataset from the table.


random() isn't immutable, so it's re-calculated for every row. Try f.ex 
this instead:
SELECT
*
FROm _g2977
ORDER BY random()
LIMIT 1


-- 
Tommy Gildseth
DBA, Gruppe for databasedrift
Universitetet i Oslo, USIT
m: +47 45 86 38 50
t: +47 22 85 29 39



view thread (4+ messages)  latest in thread

Message-ID: <46A9A524.7000009@usit.uio.no>
Permalink:  ../46A9A524.7000009@usit.uio.no/
Also on:    postgresql.org/message-id/46A9A524.7000009@usit.uio.no

 · 

reply

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tommy.gildseth@usit.uio.no, stefanzweig1881@web.de
  Subject: Re: strange result for select * from table where id=random()*900
  In-Reply-To: <46A9A524.7000009@usit.uio.no>

* 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