Re: Obtaining random rows from a result set - Mailing list pgsql-general

From Albe Laurenz
Subject Re: Obtaining random rows from a result set
Date
Msg-id D960CB61B694CF459DCFB4B0128514C22215C9@exadv11.host.magwien.gv.at
Whole thread Raw
In response to Obtaining random rows from a result set  (Alban Hertroys <alban@magproductions.nl>)
List 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

pgsql-general by date:

Previous
From: "Phoenix Kiula"
Date:
Subject: Re: URGENT: Whole DB down ("no space left on device")
Next
From: Csaba Nagy
Date:
Subject: Re: Obtaining random rows from a result set