From: | hubert depesz lubaczewski <depesz(at)depesz(dot)com> |
---|---|
To: | Kynn Jones <kynnjo(at)gmail(dot)com> |
Cc: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: GROUP BY, ORDER & LIMIT ? |
Date: | 2008-05-06 18:04:08 |
Message-ID: | 20080506180408.GA27858@depesz.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Tue, May 06, 2008 at 01:22:30PM -0400, Kynn Jones wrote:
> Suppose table X has two columns: class (TEXT) and size (INT). I want a
> listing showing the (up to) 5 largest values of "size" for each value of
> "class" (for some values of "class" the total number of available records
> may be less than 5).
> What would be the simplest way to achieve such a listing? It seems like
> something one could do with GROUP BY, ORDER, and LIMIT, but I can't figure
> out the right syntax for this query.
get rownum(in_code TEXT) function from
http://www.depesz.com/index.php/2007/08/17/rownum-anyone-cumulative-sum-in-one-query/
(it's important to use the version with in_code argument.
then write:
select *
from
(select class, size from X order by class asc size desc) q
where rownum(class) <= 5;
depesz
--
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA. here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)
From | Date | Subject | |
---|---|---|---|
Next Message | Daniel Schuchardt | 2008-05-06 18:07:35 | Re: Autocast script of peter e in PostgreSQL 8.3 |
Previous Message | David Wilson | 2008-05-06 17:55:14 | Re: GROUP BY, ORDER & LIMIT ? |