Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 62B89476DFF for ; Thu, 7 Nov 2002 13:15:34 -0500 (EST) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 321BA476B3B for ; Thu, 7 Nov 2002 13:15:15 -0500 (EST) Received: by news.hub.org (Postfix, from userid 8) id CEB5A381743; Thu, 7 Nov 2002 13:15:14 -0500 (EST) From: Jeff Boes X-Newsgroups: comp.databases.postgresql.sql Subject: Quartile (etc) ranking in a SQL statement? Date: Thu, 07 Nov 2002 13:16:44 -0500 Organization: Nexcerpt, Inc. Lines: 27 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@news.hub.org User-Agent: Pan/0.11.4 (Unix) X-Comment-To: ALL To: pgsql-sql@postgresql.org X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/116 X-Sequence-Number: 10258 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