| From: | Reece Hart <reece(at)harts(dot)net> |
|---|---|
| To: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
| Cc: | tom(at)tacocat(dot)net |
| Subject: | Re: Random Sample |
| Date: | 2007-05-18 21:16:13 |
| Message-ID: | 1179522973.6910.26.camel@snafu.site |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
On Fri, 2007-05-18 at 15:36 -0500, tom(at)tacocat(dot)net wrote:
> How do I pull a random sample of either 100 records or 5% of the
> population of a table?
If you can be a little flexible about the number of samples, you can try
select * from table where random()<=0.05;
Of course, there's nothing that guarantees that you'll get 5% and this
only works reasonably for large N. On the other hand, if N were small,
you probably wouldn't be asking for a random sample.
You could also try
select * from table order by random() limit 100;
That'll be expensive, but get you exactly 100 (if your table has >= 100
rows, of course).
-Reece
--
Reece Hart, http://harts.net/reece/, GPG:0x25EC91A0
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2007-05-18 21:26:45 | Re: JDBC - Prepared statements and PostgreSql Time/Date operations |
| Previous Message | Martin Gainty | 2007-05-18 21:02:48 | Re: JDBC - Prepared statements and PostgreSql Time/Date operations |