Group by and limit

From: Reid Thompson <reid(dot)thompson(at)ateb(dot)com>
To: pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Group by and limit
Date: 2010-11-03 00:44:39
Message-ID: 4CD0B077.2080700@ateb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Reposting as I noticed that the original was in reply to a different subject.

Hey Folks ā€“ have a coded myself into a corner yet?

I have a situation with a select count / group by / order by query that I need to limit each group to 500
entries. Not seeing a way to do this in a single query, do I need to use multiple queries?
Group x has about 200 entries in it; group y has about 5-8k per x.

select x, y, count(*) as counter from mytable
group by x, y
order by x, counter, y

I only want the first 500 for each x.

Any tips or tricks someone might know would be appreciated.

Iā€™m using postgres 8.3.7.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sheng Hui 2010-11-03 02:12:29 A strange SQL grammar issue with postgreSql 8.4.4 & Hibernate 3.5
Previous Message Jonathan Tripathy 2010-11-02 23:29:00 Re: JDBC Transactions