From: Philippe Lang <philippe.lang@attiksystem.ch>
To: pgsql-sql@postgresql.org
Subject: Re: Table transform query
Date: Tue, 18 Sep 2007 15:49:47 +0200
Message-ID: <6C0CF58A187DA5479245E0830AF84F4218CF20@poweredge.attiksystem.ch> (raw)
References: <6C0CF58A187DA5479245E0830AF84F4218CF1F@poweredge.attiksystem.ch>
pgsql-sql-owner@postgresql.org wrote:
> Hi,
>
> I'm trying to find out how to transform this kind of table data
> (history of rental data in a firm):
...
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
f.serial,
f.date as dateL,
( select f2.date from foo as f2
where f2.serial = f.serial
and f2.date > f.date
and f2.delivery = 'R'
order by f2.date asc
limit 1
) as dateR
from foo as f
where f.delivery = 'L'
order by f.serial, f.date
-------------
I'm not sure if we could use a self-join here...
Cheers,
Philippe Lang
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: philippe.lang@attiksystem.ch
Subject: Re: Table transform query
In-Reply-To: <6C0CF58A187DA5479245E0830AF84F4218CF20@poweredge.attiksystem.ch>
* 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