Quartile (etc) ranking in a SQL statement?

From: Jeff Boes <jboes(at)nexcerpt(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Quartile (etc) ranking in a SQL statement?
Date: 2002-11-07 18:16:44
Message-ID: aqeajg$pg2$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Here's a puzzler:

Given a query that returns rows ranked by some criteria, how can I write
another query around it that will give me the (say) first quartile (top
25%)? Another way of putting it is: if I have rows that look like this:

aaa | 1251
aba | 1197
cax | 1042
... | ...
axq | 23
(142 rows)

How can I write a query that will return these as

1 | aaa | 1251
2 | aba | 1197
3 | cax | 1042
... | ... | ...
142 | axq | 23

--
Jeff Boes vox 616.226.9550 ext 24
Database Engineer fax 616.349.9076
Nexcerpt, Inc. http://www.nexcerpt.com
...Nexcerpt... Extend your Expertise

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-11-07 19:09:24 Re: Weird NULL behavior
Previous Message Tom Lane 2002-11-07 18:01:31 Re: PLpgSQL FOR IN EXECUTE question