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 DA5A2D7F02 for ; Fri, 9 Sep 2005 09:22:59 -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 69318-06 for ; Fri, 9 Sep 2005 12:22:55 +0000 (GMT) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.205]) by svr1.postgresql.org (Postfix) with ESMTP id B34F2D7E49 for ; Fri, 9 Sep 2005 09:22:56 -0300 (ADT) Received: by xproxy.gmail.com with SMTP id i31so1808733wxd for ; Fri, 09 Sep 2005 05:23:00 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=GlmS8s1hTgftbUT+nkUzsC2vQYg146vK1wF9ChKg67Zi12cQPmeBmGF13vtJ2q7wBzyBn2FEI0lbahmGQbQLg2c+DU5rq/kN34whdRRMRpcgHfydwdRK2o8hDkPU9ACNpiTNQLV3GQXL4y8+O10VCMJBMoXH1hoZ/sE2QlSdRVU= Received: by 10.70.117.1 with SMTP id p1mr3887wxc; Fri, 09 Sep 2005 05:23:00 -0700 (PDT) Received: by 10.70.100.16 with HTTP; Fri, 9 Sep 2005 05:23:00 -0700 (PDT) Message-ID: Date: Fri, 9 Sep 2005 16:23:00 +0400 From: Nikolay Samokhvalov Reply-To: nikolay@samokhvalov.com To: pgsql-sql@postgresql.org Subject: SELECT: retrieve only 2 rows next to known row Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.809 required=5 tests=[AWL=0.785, RCVD_BY_IP=0.024] X-Spam-Level: X-Archive-Number: 200509/103 X-Sequence-Number: 22650 Hi, My knowledge of PostgreSQL's SQL is not good, but I know ISO/ANSI SQL:2003 (basics) quite well. 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 =3D 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. First of all, I don't want (cannot) write PL/pgSQL function. So, I should use CREATE TEMP SEQUENCE to associate all rows in result set with their order numbers (in MySQL. for example, I would use temporary variable num in SELECT: something like 'select @num :=3D @num + 1', but here I cannot, can't I?) Then, as I know, PostgreSQL doesn't support standard statement WITH, that probaby would help me with this task. 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) I'd appreciate any help. --=20 Best regards, Nikolay