X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6113AD7851 for ; Fri, 9 Sep 2005 09:38:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 75379-01 for ; Fri, 9 Sep 2005 12:38:20 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 7A68DD77EE for ; Fri, 9 Sep 2005 09:38:19 -0300 (ADT) Received: (qmail 13691 invoked by uid 500); 9 Sep 2005 12:38:30 -0000 Date: Fri, 9 Sep 2005 07:38:30 -0500 From: Bruno Wolff III To: nikolay@samokhvalov.com Cc: pgsql-sql@postgresql.org Subject: Re: SELECT: retrieve only 2 rows next to known row Message-ID: <20050909123830.GA12435@wolff.to> Mail-Followup-To: Bruno Wolff III , nikolay@samokhvalov.com, pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200509/104 X-Sequence-Number: 22651 On Fri, Sep 09, 2005 at 16:23:00 +0400, Nikolay Samokhvalov wrote: > > I've encountered with following task. I have one SELECT statement with > ORDER BY clause; and know, that result set for this SELECT contains > row with ID = 1000 (just for example). > I don't know the position of this row in result set, but I want to > retrieve 2 rows that are next to this one. > > > Any ways to solve this problem? Is it possible to make only one query? > (at least with one row in result set - e.g., with the row _following_ > after my one) You could search for the following and preceding rows of the row with an id of 1000 by using the comparison field(s) of the row in a where clause. You can use limit 1 for each of these two subselects and union the results. You may need to add the ID to the order by clause if your current ordering does not produce a unique order.