pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joost Kraaijeveld <J.Kraaijeveld@Askesis.nl>
To: pgsql-sql@postgresql.org
Subject: Can I do this smarter?
Date: Fri, 14 Jul 2006 05:41:28 +0200
Message-ID: <1152848488.6045.13.camel@localhost> (raw)

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



view thread (5+ messages)  latest in thread

Message-ID: <1152848488.6045.13.camel@localhost>
Permalink:  ../1152848488.6045.13.camel@localhost/
Also on:    postgresql.org/message-id/1152848488.6045.13.camel@localhost

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: J.Kraaijeveld@Askesis.nl
  Subject: Re: Can I do this smarter?
  In-Reply-To: <1152848488.6045.13.camel@localhost>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox