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 94C5ED1BA9B for ; Sat, 6 Sep 2003 11:07:49 +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 68786-04 for ; Sat, 6 Sep 2003 08:07:21 -0300 (ADT) Received: from lexa.uniflux-line.net (host-212.56.196.233.mldnet.com [212.56.196.233]) by svr1.postgresql.org (Postfix) with ESMTP id 7501FD1B991 for ; Sat, 6 Sep 2003 08:07:12 -0300 (ADT) Received: by lexa.uniflux-line.net (Postfix, from userid 1000) id 0035A8005; Sat, 6 Sep 2003 14:07:09 +0300 (EEST) Date: Sat, 6 Sep 2003 14:07:09 +0300 From: Alexei Chetroi Cc: Pgsql-Sql Subject: SQL subqueries newbie help Message-ID: <20030906110709.GA13384@uniflux-line.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.28i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/37 X-Sequence-Number: 15015 Hi All, I need a little help regarding writing some subqueries. For example I have a table "items" which contains columns: itemid, description; and another table "events" with columns: itemid, date, event. events.itemid references items.itemid. Table events contains events regarding some itemid's from table items, so there could be multiple events regarding one item from items table. for example: Table: items itemid description 1 Lamp 2 Desk 3 HiFi Table: events itemid event date 1 purchase 2003-01-01 1 repair 2003-01-03 1 repair 2003-02-05 2 purchase 2003-02-01 3 HiFi 2003-02-01 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? PS: are there any books regarding complex SQL queries in electronic format?