From: | "David Wilson" <david(dot)t(dot)wilson(at)gmail(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 17:55:14 |
Message-ID: | e7f9235d0805061055s6e0cef82u480d4bfa3db8a521@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
select class, size from X t1 where size in (select size from X t2
where t2.class=t1.class order by size desc limit 5);
On Tue, May 6, 2008 at 1:22 PM, Kynn Jones <kynnjo(at)gmail(dot)com> 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.
Warning, this is typed directly into mail:
select class, size from X t1 where size in (select size from X t2
where t2.class=t1.class order by size desc limit 5);
That should do the trick. The only problem is if you've got duplicated
size values, you could end up with more than 5 per class.
--
- David T. Wilson
david(dot)t(dot)wilson(at)gmail(dot)com
From | Date | Subject | |
---|---|---|---|
Next Message | hubert depesz lubaczewski | 2008-05-06 18:04:08 | Re: GROUP BY, ORDER & LIMIT ? |
Previous Message | Kynn Jones | 2008-05-06 17:22:30 | GROUP BY, ORDER & LIMIT ? |