pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: nikolay@samokhvalov.com
Cc: pgsql-sql@postgresql.org
Subject: Re: SELECT: retrieve only 2 rows next to known row
Date: Fri, 9 Sep 2005 07:38:30 -0500
Message-ID: <20050909123830.GA12435@wolff.to> (raw)
In-Reply-To: <e431ff4c050909052360490fc2@mail.gmail.com>
References: <e431ff4c050909052360490fc2@mail.gmail.com>

On Fri, Sep 09, 2005 at 16:23:00 +0400,
  Nikolay Samokhvalov <samokhvalov@gmail.com> 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.



view thread (4+ messages)  latest in thread

Message-ID: <20050909123830.GA12435@wolff.to>
Permalink:  ../20050909123830.GA12435@wolff.to/
Also on:    postgresql.org/message-id/20050909123830.GA12435@wolff.to

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bruno@wolff.to, nikolay@samokhvalov.com
  Subject: Re: SELECT: retrieve only 2 rows next to known row
  In-Reply-To: <20050909123830.GA12435@wolff.to>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox