X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id B42A14759BD for ; Wed, 12 Feb 2003 14:57:30 -0500 (EST) Received: from localhost.localdomain (Ad6dd.pppool.de [213.6.214.221]) by postgresql.org (Postfix) with ESMTP id 6E599474E44 for ; Wed, 12 Feb 2003 14:57:28 -0500 (EST) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.11.6/8.11.6) with ESMTP id h1CJtMa05285; Wed, 12 Feb 2003 20:55:23 +0100 Content-Type: text/plain; charset="iso-8859-1" From: Nicholas Allen To: Bruno Wolff III Subject: Re: How do you select from a table until a condition is met? Date: Wed, 12 Feb 2003 20:55:21 +0100 User-Agent: KMail/1.4.1 Cc: Dmitry Tkach , pgsql-sql@postgresql.org References: <200302121910.38232.nallen@freenet.co.uk> <200302122010.15297.nallen@freenet.co.uk> <20030212193538.GA29724@wolff.to> In-Reply-To: <20030212193538.GA29724@wolff.to> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200302122055.21790.nallen@freenet.co.uk> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/249 X-Sequence-Number: 11647 I thought of this but the problem is that there may be multiple rows with t= he=20 same value for the column I am sorting on. Eg if sorting on a surname then= =20 there may be 100s of people with the same surname so generating a where=20 clause that selects up to the exact person previously selected is very=20 difficult. eg where surname <=3D 'Jones' could return hundreds of records and probably= some=20 records for people with the same surname but which appear after the selecte= d=20 record. I had another idea though. Do you know if it is possible to write a functio= n=20 for postgresql thatwill return true until a condition is met and then for= =20 every row after that return false? This way I could write a function that= =20 could select up to a particular row using the where clause and calling the= =20 function from the where claus. I'm sure there must be a way of doing this as it is such an obvious thing t= o=20 need to do... Thanks for the help! On Wednesday 12 Feb 2003 8:35 pm, Bruno Wolff III wrote: > 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=3Dvalue1 && > > col2=3Dval2 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.