pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: scott.marlowe <scott.marlowe@ihs.com>
To: Iain <iain@mst.co.jp>
Cc: Enver ALTIN <enver.altin@frontsite.com.tr>
Cc: pgsql-sql@postgresql.org
Subject: Re: randomized order in select?
Date: Thu, 11 Mar 2004 09:28:43 -0700 (MST)
Message-ID: <Pine.LNX.4.33.0403110924161.11329-100000@css120.ihs.com> (raw)
In-Reply-To: <009301c4072a$19b28fd0$7201a8c0@mst1x5r347kymb>

On Thu, 11 Mar 2004, Iain wrote:

> If you have a lot of tips, you could create a unique indexed tip number
> column. Select the highest tip number using:
> 
> select tip_number from tips order by tip_number desc limit 1;
> 
> Then generate a random number and select using that tip_number.
> 
> Of course, you would have to allow for the possibility of missing tip
> numbers, by repeating the random number generation/read sequence until you
> find something. Since the tip_number isn't the PK of the table, you can
> regenerate the tip numbers to eliminate holes from deletions any time you
> like. Just reset the sequence to 1 and update all rows with the
> nextval(tipnumber_seq).
> 
> Sounds like a lot of work to me though...

Assuming there are ten rows, you can use this:

select * from table limit 1 offset random()*10;





view thread (7+ messages)

Message-ID: <Pine.LNX.4.33.0403110924161.11329-100000@css120.ihs.com>
Permalink:  ../Pine.LNX.4.33.0403110924161.11329-100000@css120.ihs.com/
Also on:    postgresql.org/message-id/Pine.LNX.4.33.0403110924161.11329-100000@css120.ihs.com

 · 

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: scott.marlowe@ihs.com, iain@mst.co.jp, enver.altin@frontsite.com.tr
  Subject: Re: randomized order in select?
  In-Reply-To: <Pine.LNX.4.33.0403110924161.11329-100000@css120.ihs.com>

* 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