Re: Obtaining random rows from a result set

From: "Albe Laurenz" <all(at)adv(dot)magwien(dot)gv(dot)at>
To: "Alban Hertroys *EXTERN*" <alban(at)magproductions(dot)nl>, "Postgres General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Obtaining random rows from a result set
Date: 2007-08-31 13:38:57
Message-ID: D960CB61B694CF459DCFB4B0128514C22215C9@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alban Hertroys wrote:
> I've recently been busy improving a query that yields a fixed
> number of random records matching certain conditions.

> Dear Santa,
>
> I'd like my database to have functionality analogue to how
> LIMIT works,
> but for other - non-sequential - algorithms.
>
> I was thinking along the lines of:
>
> SELECT *
> FROM table
> WHERE condition = true
> RANDOM 5;

Ho, ho, ho.

SELECT *
FROM table
WHERE condition = true
ORDER BY hashfloat8(random())
LIMIT 5;

Yours,
Laurenz Albe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Csaba Nagy 2007-08-31 13:45:47 Re: Obtaining random rows from a result set
Previous Message Phoenix Kiula 2007-08-31 13:35:09 Re: URGENT: Whole DB down ("no space left on device")