Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id DE8A39F95B6 for ; Tue, 18 Sep 2007 10:50:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 77411-04 for ; Tue, 18 Sep 2007 10:50:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.attiksystem.ch (f29.attiksystem.ch [212.147.59.29]) by postgresql.org (Postfix) with ESMTP id 5485E9F9B16 for ; Tue, 18 Sep 2007 10:49:50 -0300 (ADT) Received: from poweredge.attiksystem.ch (poweredge.attiksystem.ch [10.0.0.29]) by mail.attiksystem.ch (8.12.11/8.12.11) with ESMTP id l8IDnlan098602 for ; Tue, 18 Sep 2007 15:49:48 +0200 (CEST) (envelope-from philippe.lang@attiksystem.ch) Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Table transform query Date: Tue, 18 Sep 2007 15:49:47 +0200 X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Message-ID: <6C0CF58A187DA5479245E0830AF84F4218CF20@poweredge.attiksystem.ch> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Table transform query thread-index: Acf59ZVMHKWd76BHSS2ChG5uHC6axgABAV8Q References: <6C0CF58A187DA5479245E0830AF84F4218CF1F@poweredge.attiksystem.ch> From: "Philippe Lang" To: X-Scanned-By: MIMEDefang 2.49 on 10.0.0.111 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/130 X-Sequence-Number: 29343 pgsql-sql-owner@postgresql.org wrote: > Hi, >=20 > I'm trying to find out how to transform this kind of table data > (history of rental data in a firm):=20 ... =20 I have answred my own question: yes, there is a pure SQL solution, with a subselect: CREATE TABLE foo ( serial integer, delivery character(1), date integer ); INSERT INTO foo (serial, delivery, date) VALUES (1, 'L', 1); INSERT INTO foo (serial, delivery, date) VALUES (2, 'L', 1); INSERT INTO foo (serial, delivery, date) VALUES (3, 'L', 1); INSERT INTO foo (serial, delivery, date) VALUES (1, 'R', 2); INSERT INTO foo (serial, delivery, date) VALUES (2, 'R', 2); INSERT INTO foo (serial, delivery, date) VALUES (4, 'L', 2); INSERT INTO foo (serial, delivery, date) VALUES (5, 'L', 3); INSERT INTO foo (serial, delivery, date) VALUES (3, 'R', 3); INSERT INTO foo (serial, delivery, date) VALUES (4, 'R', 3); INSERT INTO foo (serial, delivery, date) VALUES (1, 'L', 4); ------------- select=20 f.serial,=20 f.date as dateL, ( select f2.date from foo as f2=20 where f2.serial =3D f.serial=20 and f2.date > f.date=20 and f2.delivery =3D 'R'=20 order by f2.date asc=20 limit 1 ) as dateR from foo as f where f.delivery =3D 'L' order by f.serial, f.date ------------- I'm not sure if we could use a self-join here... Cheers, Philippe Lang