Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 7D8E79FA445 for ; Fri, 21 Sep 2007 05:25:52 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 79477-05-2 for ; Fri, 21 Sep 2007 05:25:37 -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 D80DD9FA05A for ; Fri, 21 Sep 2007 05:25:39 -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 l8L8Pbw3019266; Fri, 21 Sep 2007 10:25:37 +0200 (CEST) (envelope-from philippe.lang@attiksystem.ch) Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: Re: Table transform query Date: Fri, 21 Sep 2007 10:25:36 +0200 X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Message-ID: <6C0CF58A187DA5479245E0830AF84F4218CF26@poweredge.attiksystem.ch> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Table transform query thread-index: Acf6lRinZ5FTruZGRtCVYdaqAbIBHgBkvwTw References: <6C0CF58A187DA5479245E0830AF84F4218CF1F@poweredge.attiksystem.ch> From: "Philippe Lang" To: =?iso-8859-1?Q?Nis_J=F8rgensen?= , X-Scanned-By: MIMEDefang 2.49 on 10.0.0.111 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/137 X-Sequence-Number: 29350 > A take on a self-join: >=20 > SELECT t1.serial, t1.date as dateL, MIN(t2.date) as dateR FROM t t1 > LEFT JOIN t t2 ON t1.serial =3D t2.serial AND t1.date < t2.date AND > t2.delivery =3D 'R' =20 > WHERE t1.delivery =3D 'L' > GROUP BY t1.serial, t1.date >=20 > Whether this is any clearer, or runs faster, than the correlated > subquery (which could be simplified by using MIN instead of LIMIT 1) > is up for debate and test, respectively. =20 Hi Nis, Thanks for your tip with the "MIN" operator. I always imagined a self-join solution was faster than a query with a = subselect. With a quick test, it seems to be the case here. 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); -- Subselect SELECT=20 f.serial, f.date as dateL, ( SELECT MIN(f2.date) FROM foo AS f2 WHERE f2.serial =3D f.serial AND f2.date > f.date AND f2.delivery =3D 'R'=20 ) AS dateR FROM foo AS f WHERE f.delivery =3D 'L' ORDER BY f.serial, f.date -- Self-join SELECT=20 t1.serial,=20 t1.date as dateL,=20 MIN(t2.date) as dateR=20 FROM foo t1=20 LEFT JOIN foo t2=20 ON t1.serial =3D t2.serial=20 AND t1.date < t2.date=20 AND t2.delivery =3D 'R' WHERE t1.delivery =3D 'L' GROUP BY t1.serial, t1.date ORDER BY t1.serial, t1.date