pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug? Me or PostgreSQL.
3+ messages / 3 participants
[nested] [flat]

* Bug? Me or PostgreSQL.
@ 2001-02-19 04:45 Christopher Sawtell <csawtell@xtra.co.nz>
  2001-02-19 05:37 ` Re: Bug? Me or PostgreSQL. Tom Lane <tgl@sss.pgh.pa.us>
  2001-02-19 05:47 ` Re: Bug? Me or PostgreSQL. Tod McQuillin <devin@spamcop.net>
  0 siblings, 2 replies; 3+ messages in thread

From: Christopher Sawtell @ 2001-02-19 04:45 UTC (permalink / raw)
  To: pgsql-sql

Greetings,

How can this happen?

chris=# select name from boys_names where 
boys_names.number=(random()*225+1)::int4;
  name
--------
 Fred
 Gunnar
 Manuel
 Rainer
(4 rows)

Here is the table declaration:

create table boys_names (
        number                  serial primary key,
        name                    varchar(20)
        ); 

the details of my installation:-

chris=# select version();
                                version
------------------------------------------------------------------------
 PostgreSQL 7.1beta3 on i586-pc-linux-gnu, compiled by GCC egcs-2.91.66
(1 row)

-- 
Sincerely etc.,

 NAME       Christopher Sawtell
 CELL PHONE 021 257 4451
 ICQ UIN    45863470
 EMAIL      csawtell @ xtra . co . nz
 CNOTES     ftp://ftp.funet.fi/pub/languages/C/tutorials/sawtell_C.tar.gz

 -->> Please refrain from using HTML or WORD attachments in e-mails to me 
<<--




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Bug? Me or PostgreSQL.
  2001-02-19 04:45 Bug? Me or PostgreSQL. Christopher Sawtell <csawtell@xtra.co.nz>
@ 2001-02-19 05:37 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2001-02-19 05:37 UTC (permalink / raw)
  To: Christopher Sawtell <csawtell@xtra.co.nz>; +Cc: pgsql-sql

Christopher Sawtell <csawtell@xtra.co.nz> writes:
> How can this happen?

> chris=# select name from boys_names where 
> boys_names.number=(random()*225+1)::int4;
>   name
> --------
>  Fred
>  Gunnar
>  Manuel
>  Rainer
> (4 rows)

Er, what's your complaint exactly?  It's not obvious to me that
there's anything wrong here.  You seem to have gotten random
matches on four rows --- if that's ridiculously too low (or too
high) you'd better explain why you think so.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Bug? Me or PostgreSQL.
  2001-02-19 04:45 Bug? Me or PostgreSQL. Christopher Sawtell <csawtell@xtra.co.nz>
@ 2001-02-19 05:47 ` Tod McQuillin <devin@spamcop.net>
  1 sibling, 0 replies; 3+ messages in thread

From: Tod McQuillin @ 2001-02-19 05:47 UTC (permalink / raw)
  To: Christopher Sawtell <csawtell@xtra.co.nz>; +Cc: pgsql-sql

On Mon, 19 Feb 2001, Christopher Sawtell wrote:

> How can this happen?
>
> chris=# select name from boys_names where
> boys_names.number=(random()*225+1)::int4;
>   name
> --------
>  Fred
>  Gunnar
>  Manuel
>  Rainer
> (4 rows)

It's not clear what you expected to see -- I'll guess you expected only
one row returned.

But random() is being called once for every row in the table -- it's not
surprising (if you have a lot of rows) that it comes up with a match more
than once.  Sometimes it might even return no rows at all.

SQL is not the best language for picking random members of a set.  You're
better off picking a random number in a procedural language and passing
that number as a constant to an embedded SQL query.
-- 
Tod McQuillin





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-02-19 05:47 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-19 04:45 Bug? Me or PostgreSQL. Christopher Sawtell <csawtell@xtra.co.nz>
2001-02-19 05:37 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-19 05:47 ` Tod McQuillin <devin@spamcop.net>

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