a JOIN on same table, but 'slided over' - Mailing list pgsql-general

From Rafal Pietrak
Subject a JOIN on same table, but 'slided over'
Date
Msg-id 1182841834.28091.34.camel@zorro.isa-geek.com
Whole thread Raw
Responses Re: a JOIN on same table, but 'slided over'  ("hubert depesz lubaczewski" <depesz@gmail.com>)
Re: a JOIN on same table, but 'slided over'  (PFC <lists@peufeu.com>)
List pgsql-general
Hi,

I understand, that this is 'general SQL' question rather then 'general
postgres'. But may be someone here could help me with it anyways.

I have a *single* table:

CREATE TABLE test (id int not null unique, thread int not null, info
text);

The ID, although unique, is not continues. A sample query:
----------------------------------------
SELECT * from test;
 id | thread | info
----+--------+------
  2 |    763 | A
  3 |    764 | B
  6 |      5 | C
  8 |  88946 | Cats
  9 |  69315 | Eifel
 10 |  96379 | G
 14 |  23927 | test 1
 16 |  16529 | test 2
 17 |    634 | test 3
 20 |  63930 | batman
(10 rows)
-----------------------------------------

Now, I'd like to make a JOIN-ed query of that table with itself, so that
I'd get rows paiwise: every row containing data from *two* rows of the
original TEST table so, that those data come from rows of consequtive
ID's - not neceserly (depending on the TEST table contents) continuesly
consequtive. Like:

SELECT * from view_of_test;
 id | id+X | thread | thread+X | info  | info+X
----+------+--------+----------+-------+---------
  2 |    3 |    763 |      764 | A     | B
  3 |    6 |    764 |        5 | B     | C
  6 |    8 |      5 |    88946 | C     | Cats
  8 |    9 |  88946 |    69315 | Cats  | Eifel
  9 |   10 |  69315 |    96379 | Eifel | G
-------------------------------------------------
Is there an SQL construct to get it?

I'd apreciate any hints or sugestions.

-R

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: varchar(n) VS text
Next
From: "hubert depesz lubaczewski"
Date:
Subject: Re: a JOIN on same table, but 'slided over'