Re: Selecting top N percent of records. - Mailing list pgsql-general

From Brent Wood
Subject Re: Selecting top N percent of records.
Date
Msg-id 4CBC72640200007B00030A45@gwia.niwa.co.nz
Whole thread Raw
In response to Selecting top N percent of records.  (Tim Uckun <timuckun@gmail.com>)
List pgsql-general
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 <where clause>
order by c desc
limit (select count(*)/10 from table where <same where clause>);


where c is the no of sales column




Brent Wood
DBA/GIS consultant
NIWA, Wellington
New Zealand

>>> Tim Uckun <timuckun@gmail.com> 10/18/10 3:40 PM >>>
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/pgsql-general

NIWA is the trading name of the National Institute of Water & Atmospheric Research Ltd.

pgsql-general by date:

Previous
From: Greg Smith
Date:
Subject: Re: installing from source in Windows
Next
From: guti deng
Date:
Subject: How to track error messages come from plpy.execute?