Re: random rows

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Jie Liang <jliang(at)ipinc(dot)com>, sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: random rows
Date: 2001-04-26 19:34:31
Message-ID: 3AE87847.F943E9E6@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-general pgsql-sql

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(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message J.H.M. Dassen Ray 2001-04-26 19:53:32 Re: random rows
Previous Message Jie Liang 2001-04-26 19:29:22 Re: random rows

Browse pgsql-general by date

  From Date Subject
Next Message J.H.M. Dassen Ray 2001-04-26 19:37:10 Re: crypt(table.field) ?
Previous Message Marc SCHAEFER 2001-04-26 19:34:09 Re: Why Size Of Data Backed Up Varies Significantly In SQL 6.5?

Browse pgsql-sql by date

  From Date Subject
Next Message J.H.M. Dassen Ray 2001-04-26 19:53:32 Re: random rows
Previous Message Jie Liang 2001-04-26 19:29:22 Re: random rows