X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 51D79475CB4 for ; Wed, 12 Feb 2003 14:22:19 -0500 (EST) Received: from wolff.to (wolff.to [66.93.249.74]) by postgresql.org (Postfix) with SMTP id C231D475C9E for ; Wed, 12 Feb 2003 14:22:16 -0500 (EST) Received: (qmail 29792 invoked by uid 500); 12 Feb 2003 19:35:38 -0000 Date: Wed, 12 Feb 2003 13:35:38 -0600 From: Bruno Wolff III To: Nicholas Allen Cc: Dmitry Tkach , pgsql-sql@postgresql.org Subject: Re: How do you select from a table until a condition is met? Message-ID: <20030212193538.GA29724@wolff.to> Mail-Followup-To: Nicholas Allen , Dmitry Tkach , pgsql-sql@postgresql.org References: <200302121910.38232.nallen@freenet.co.uk> <3E4AB0E7.3010101@openratings.com> <200302122010.15297.nallen@freenet.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200302122010.15297.nallen@freenet.co.uk> User-Agent: Mutt/1.3.25i X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/248 X-Sequence-Number: 11646 On Wed, Feb 12, 2003 at 20:10:15 +0100, Nicholas Allen wrote: > > Thanks, but I don't think thats what I want. Basically I need to get the row > offset of a record (ie row) in a result set returned from the database. I > know all the details of the row in question but when the search order changes > I need to know the new index the row will have. > > In other words, I was hoping I would be able to select upto a particular row > (not knowing its offset but knowing everything else about it) in a select > query. > > Ideally I could do something like SELECT count(*) UNTIL col1=value1 && > col2=val2 etc.... Since you are ordering the data you can use where clauses with < (and/or >) operators to stop counting at the desired row. For this to work the ordering needs to be well defined. You can tack on the primary key to your list of columns to order by to make the ordering well defined if it isn't already.