X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id D06349FB365 for ; Fri, 14 Jul 2006 00:41:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 70071-08 for ; Fri, 14 Jul 2006 00:41:31 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from obelix.askesis.nl (laudanum.demon.nl [82.161.125.16]) by postgresql.org (Postfix) with ESMTP id 0AAAF9FB2DB for ; Fri, 14 Jul 2006 00:41:31 -0300 (ADT) Received: obelix 172.31.0.1 from 172.31.1.1 172.31.1.1 via HTTP with MS-WebStorage 6.0.6249 Received: from panoramix by obelix; 14 Jul 2006 05:41:29 +0200 Subject: Can I do this smarter? From: Joost Kraaijeveld To: pgsql-sql@postgresql.org Content-Type: text/plain Content-Transfer-Encoding: 7bit Date: Fri, 14 Jul 2006 05:41:28 +0200 Message-Id: <1152848488.6045.13.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.6.2 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/207 X-Sequence-Number: 25781 I have three tables: customers, salesorders and invoices. Customers have salesorders and salesorders have invoices ( child tables have foreign key columns to their parent). I want to get a list of all invoices with their customers. This what I came up with: select invoices.objectid, invoices.invoicenumber, invoices.invoicedate, (select customer from salesorders where objectid = invoices.salesorderobjectid), (select customernumber from customers where objectid = (select customer from salesorders where objectid = invoices.salesorderobjectid)), (select lastname from customers where objectid = (select customer from salesorders where objectid = invoices.salesorderobjectid)) from invoices Can I do this smarter as the three subselects select the same customer three times and I would think that 1 time is enough? TIA -- Groeten, Joost Kraaijeveld Askesis B.V. Molukkenstraat 14 6524NB Nijmegen tel: 024-3888063 / 06-51855277 fax: 024-3608416 web: www.askesis.nl