select random row from a group - Mailing list pgsql-general

From Thomas T. Thai
Subject select random row from a group
Date
Msg-id Pine.NEB.4.44.0304080157180.7952-100000@ns01.minnesota.com
Whole thread Raw
Responses Re: select random row from a group
List pgsql-general
I'd like to be able to select one random row from each group using one
select statement.

CREATE TABLE randtest (
  catnum int,
  title varchar(32)
);

INSERT INTO randtest VALUES (1, 'one.one');
INSERT INTO randtest VALUES (1, 'one.two');
INSERT INTO randtest VALUES (1, 'one.three');
INSERT INTO randtest VALUES (2, 'two.one');
INSERT INTO randtest VALUES (2, 'two.two');
INSERT INTO randtest VALUES (2, 'two.three');
INSERT INTO randtest VALUES (3, 'three.one');
INSERT INTO randtest VALUES (3, 'three.two');
INSERT INTO randtest VALUES (3, 'three.three');

Something along the line of:

SELECT catnum, title, MAX(RAND()) as r
FROM randtest
WHERE 1=1
GROUP BY catnum;

--
Thomas T. Thai


pgsql-general by date:

Previous
From: "J. M. Brenner"
Date:
Subject: Re: Failed dependencies: perl(Pg) is needed by postgresql-contrib
Next
From: "martin"
Date:
Subject: Instalation problem