Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DD722476082 for ; Thu, 7 Nov 2002 15:49:29 -0500 (EST) Received: from beamish.nsd.ca (unknown [205.150.156.194]) by postgresql.org (Postfix) with ESMTP id 10CC4475F54 for ; Thu, 7 Nov 2002 15:49:29 -0500 (EST) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id PAA28145; Thu, 7 Nov 2002 15:49:28 -0500 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma028140; Thu, 7 Nov 02 15:49:23 -0500 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id PAA00508; Thu, 7 Nov 2002 15:48:35 -0500 Message-ID: <3DCAD1FA.7CB51F7@nsd.ca> Date: Thu, 07 Nov 2002 15:50:02 -0500 From: Jean-Luc Lachance X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9-31 i686) X-Accept-Language: en MIME-Version: 1.0 To: Jeff Boes Cc: pgsql-sql@postgresql.org Subject: Re: Quartile (etc) ranking in a SQL statement? References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/120 X-Sequence-Number: 10262 In a PLPGPSQL script, once you know count(*) try execute ''select * from table limit '' || int4( theCount / 4); If you want numbering, create a sequence and add nextval() to the query. JLL Jeff Boes wrote: > > 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 > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly