Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id BC71A9FA1FF for ; Tue, 18 Sep 2007 10:26:51 -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 72797-06 for ; Tue, 18 Sep 2007 10:26:48 -0300 (ADT) X-Greylist: delayed 00:14:20.249909 by SQLgrey-1.7.5 Received: from mail.attiksystem.ch (f29.attiksystem.ch [212.147.59.29]) by postgresql.org (Postfix) with ESMTP id 90C609FA16E for ; Tue, 18 Sep 2007 10:26:48 -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 l8IDCPNx098410 for ; Tue, 18 Sep 2007 15:12:25 +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: Table transform query Date: Tue, 18 Sep 2007 15:12:25 +0200 X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Message-ID: <6C0CF58A187DA5479245E0830AF84F4218CF1F@poweredge.attiksystem.ch> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Table transform query thread-index: Acf59ZVMHKWd76BHSS2ChG5uHC6axg== From: "Philippe Lang" To: X-Scanned-By: MIMEDefang 2.49 on 10.0.0.111 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.118 tagged_above=0 required=5 tests=AWL=0.117, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200709/129 X-Sequence-Number: 29342 Hi, I'm trying to find out how to transform this kind of table data (history of rental data in a firm): date serial delivery ---------------------- 1 1 L 1 2 L 1 3 L 2 1 R 2 2 R 2 4 L 3 5 L 3 3 R 3 4 R 4 1 L ... into this: serial dateL dateR -------------------- 1 1 2 1 4 =20 2 1 2 3 1 3 4 2 3 5 3 =20 Basically, data on table 1 means:=20 - on date 1, product with serial 1 is sent to the customer - on date 1, product with serial 2 is sent to the customer - on date 1, product with serial 3 is sent to the customer - on date 2, product with serial 1 comes back ... On table 2, data means: - Product with serial 1 is sent to the customer on date 1, and comes back on date 2 - Product with serial 1 is sent to the customer on date 4, and hasn't come back yet ... Do you think there is a generic SQL solution to this problem, like crosstab or pivot table? I'm thinking of doing things in a "procedural" plpgsql manner... Any idea is welcome. Thanks! Philippe