X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 26925D1B4F7 for ; Sat, 6 Sep 2003 17:22:00 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 95621-02 for ; Sat, 6 Sep 2003 14:21:29 -0300 (ADT) Received: from mailav.tor1.inquent.com (mail.inquent.com [216.208.117.106]) by svr1.postgresql.org (Postfix) with ESMTP id 29176D1B508 for ; Sat, 6 Sep 2003 14:21:28 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mailav.tor1.inquent.com (Postfix) with ESMTP id 287B01025C; Sat, 6 Sep 2003 13:13:03 -0400 (EDT) Received: from [192.168.1.200] (dyn-133-83.tor.dsl.tht.net [134.22.133.83]) by mailav.tor1.inquent.com (Postfix) with ESMTP id DEB4B1025A; Sat, 6 Sep 2003 13:13:00 -0400 (EDT) Subject: Re: SQL subqueries newbie help From: Rod Taylor To: Alexei Chetroi Cc: Pgsql-Sql In-Reply-To: <20030906110709.GA13384@uniflux-line.net> References: <20030906110709.GA13384@uniflux-line.net> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-IRrPZC3JwCzGC0rVuhlS" Message-Id: <1062868895.54737.3.camel@jester> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.4 Date: Sat, 06 Sep 2003 13:21:36 -0400 X-Virus-Scanned: by AMaViS perl-11 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/39 X-Sequence-Number: 15017 --=-IRrPZC3JwCzGC0rVuhlS Content-Type: text/plain Content-Transfer-Encoding: quoted-printable > I'd like to write a query which returns following information regarding > each item: item, date of very first event, very last event. > Is this possible? I think I can write several SELECT queries and > procces them by an application or possibly write some procedure, but > what is better solution? Something like the below should do it: SELECT item , min_date , min_event , max_date , max_event FROM items JOIN (SELECT min(date) AS min_date , event AS min_event , item FROM events GROUP BY item) AS mn USING (item) JOIN (SELECT max(date) AS max_date , event AS max_event , item FROM events GROUP BY item) AS mx USING (item); --=-IRrPZC3JwCzGC0rVuhlS Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3 (FreeBSD) iD8DBQA/Whef6DETLow6vwwRAtQ4AJwOQyEtCldDVpO+yDTROfeLpCXX9gCfbWXB 2tnjFgqccnRcQ9aOBf2H5OI= =BugX -----END PGP SIGNATURE----- --=-IRrPZC3JwCzGC0rVuhlS--