Can I do this smarter? - Mailing list pgsql-sql

From Joost Kraaijeveld
Subject Can I do this smarter?
Date
Msg-id 1152848488.6045.13.camel@localhost
Whole thread Raw
Responses Re: Can I do this smarter?  ("Aaron Bono" <postgresql@aranya.com>)
List pgsql-sql
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


pgsql-sql by date:

Previous
From: "Aaron Bono"
Date:
Subject: Re: Trigger, record "old" is not assigned yet
Next
From: "Aaron Bono"
Date:
Subject: Re: Can I do this smarter?