Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql General > Re: GROUP BY, O...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 15471 of 17437
Post > Topic >>

Re: GROUP BY, ORDER & LIMIT ?

by david.t.wilson@[EMAIL PROTECTED] ("David Wilson") May 6, 2008 at 01:55 PM

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@[EMAIL PROTECTED]
> 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.t.wilson@[EMAIL PROTECTED]
 
Sent via pgsql-general mailing list (pgsql-general@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
 




 3 Posts in Topic:
GROUP BY, ORDER & LIMIT ?
kynnjo@[EMAIL PROTECTED]   2008-05-06 13:22:30 
Re: GROUP BY, ORDER & LIMIT ?
david.t.wilson@[EMAIL PRO  2008-05-06 13:55:14 
Re: GROUP BY, ORDER & LIMIT ?
depesz@[EMAIL PROTECTED]   2008-05-06 20:04:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 13:11:34 CST 2008.