From: Josh Berkus <josh@agliodbs.com>
To: Jie Liang <jliang@ipinc.com>
To: sqllist <pgsql-sql@postgresql.org>
Subject: Re: random rows
Date: Thu, 26 Apr 2001 12:34:31 -0700
Message-ID: <3AE87847.F943E9E6@agliodbs.com> (raw)
References: <Pine.BSF.4.10.10104261133340.77756-100000@tidal.ipinc.com>
Jie,
> How I can return random N rows from my select stmt?
> like:
> e.g. what my selectee is a set of 1000 rows, I want randomly
> pickup 100 of 1000.
You'd have to do it inside a function or external program, and copy the
rows to a temporary table (which is what you'd return to the user).
Thus, language-agnostic rules:
CREATE FUNCTION return_random(X)
LOOP X Times
totalrecs = COUNT(*) FROM maintable WHERE NOT EXISTS temptable
offset_count = RANDOM*totalrecs
INSERT INTO temptable ( a, b, c, d )
SELECT a, b, c, d FROM maintable
LIMIT 1 OFFSET offset_count;
END LOOP
END;
than:
SELECT temptable
-Josh
--
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh@agliodbs.com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: josh@agliodbs.com, jliang@ipinc.com
Subject: Re: random rows
In-Reply-To: <3AE87847.F943E9E6@agliodbs.com>
References: <Pine.BSF.4.10.10104261133340.77756-100000@tidal.ipinc.com> <3AE87847.F943E9E6@agliodbs.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