X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D02F88BA12F for ; Fri, 11 Feb 2005 11:40:03 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 83965-04 for ; Fri, 11 Feb 2005 11:39:49 +0000 (GMT) Received: from pw6tiger.de (pw6tiger.de [217.160.140.116]) by svr1.postgresql.org (Postfix) with SMTP id 3904D8B9C44 for ; Fri, 11 Feb 2005 11:39:49 +0000 (GMT) Received: (qmail 22756 invoked by uid 505); 11 Feb 2005 11:39:48 -0000 Received: from vygen@gmx.de by planwerk6 by uid 89 with qmail-scanner-1.14 (virus scan: Clear:. Processed in 0.20732 secs); 11 Feb 2005 11:39:48 -0000 Received: from isi-dial-146-230.isionline-dialin.de (HELO jv.pw6.de) (vygen@planwerk6.de@195.158.146.230) by pw6tiger.de with SMTP; 11 Feb 2005 11:39:48 -0000 From: Janning Vygen Organization: PLANWERK 6 To: David Goodenough Subject: Re: SQL query Date: Fri, 11 Feb 2005 12:41:06 +0100 User-Agent: KMail/1.7.1 Cc: pgsql-general@postgresql.org References: <200502111107.25004.david.goodenough@btconnect.com> In-Reply-To: <200502111107.25004.david.goodenough@btconnect.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200502111241.06483.vygen@gmx.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.025 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200502/633 X-Sequence-Number: 73437 Am Freitag, 11. Februar 2005 12:07 schrieb David Goodenough: > > I have an address table, with all the normal fields and a customer name > field and an address type. There is a constraint that means that the > combination of customer and type have to be unique. Normally the > only record per customer will be of type 'default', but if for instance > the customer wants a different billing address I would add in a second > type='billing' address record. > > I then want to join this table to another table, say an invoice table, > and I want to use the billing address if present, otherwise the default > address. I do not want to create either two addresses or to put both > addresses on the invoice. > > I could do this by doing a select * from addresses where customer = ? > and type = 'billing', looking to see if there is a result row and if not > repeating the query with type = 'default', but that seems inelegant to > me. > > I thought of using an inner select for the join, and using limit 1 to > get just the one, and forcing the order by to give me the billing > address by preference, but I am then dependant on the sort order > of the particular type values I am selecting from. don't think "vertical" (adresses in rows), think "horizontal" (adresses in columns), like this: SELECT c.*, COALESCE(a1.street, a2.street) AS street, COALESCE(a1.zip, a2.zip) AS zip, COALESCE(a1.town, a2.town) AS town FROM customer AS c LEFT JOIN adresses AS a1 USING (customer_id) LEFT JOIN adresses AS a2 USING (customer_id) WHERE a1.type = default AND a2.type = 'billing' i just type the and did not tested it. the trick is to join adresses multiple times and get the right data with COALESCE function which returns the first value which is NOT NULL. If you still have difficulties, please send your schema. kind regards, janning