Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Brent Wood
Something like this should work - (but is untested), and does require the extra subquery, so there may be a more efficient way? However, off the top of my head: select a,b,c from table where order by c desc limit (select count(*)/10 from table where ); where c is the no of sales column B

Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Tim Uckun
> > OK, so you want a median-style "sort them in descending order and count down > until you've selected the first 10% of rows" approach? In other words, > values in the 90th percentile of the distribution? > > Try this. Given table "x" with single integer column "y", obtain rows of x > in the 90th

Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Craig Ringer
On 10/18/2010 08:06 AM, Tim Uckun wrote: That is a bit problematic because it necessitates knowing the number of rows total, and slow counting is an idiosyncrasy of postgres. http://wiki.postgresql.org/wiki/Slow_Counting To get the top 10%: SELECT * FROM table LIMIT(SELECT (COUNT(*) * 0.1)::in

Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Tim Uckun
> That is a bit problematic because it necessitates knowing the number > of rows total, and slow counting is an idiosyncrasy of postgres. > > http://wiki.postgresql.org/wiki/Slow_Counting > > To get the top 10%: > > SELECT * FROM table LIMIT(SELECT (COUNT(*) * 0.1)::integer FROM table) I think I

Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Tim Uckun
On Mon, Oct 18, 2010 at 12:53 PM, Darren Duncan wrote: > Tim Uckun wrote: >> >> Is there a way to select the top 10% of the values from a column? >> >> For example the top 10% best selling items where number of sales is a >> column. > > The top 10% would be a variable number of records.  Is that w

Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Peter Geoghegan
On 18 October 2010 00:33, Tim Uckun wrote: > Is there a way to select the top 10% of the values from a column? > > For example the top 10% best selling items where number of sales is a column. > That is a bit problematic because it necessitates knowing the number of rows total, and slow counting

Re: [GENERAL] Selecting top N percent of records.

2010-10-17 Thread Darren Duncan
Tim Uckun wrote: Is there a way to select the top 10% of the values from a column? For example the top 10% best selling items where number of sales is a column. The top 10% would be a variable number of records. Is that what you want, or would you rather, say, just see the top N items? The

[GENERAL] Selecting top N percent of records.

2010-10-17 Thread Tim Uckun
Is there a way to select the top 10% of the values from a column? For example the top 10% best selling items where number of sales is a column. Thanks. -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/