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 B2BF78BA086 for ; Fri, 11 Feb 2005 11:46:36 +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-09 for ; Fri, 11 Feb 2005 11:46:20 +0000 (GMT) Received: from anchor-post-36.mail.demon.net (anchor-post-36.mail.demon.net [194.217.242.86]) by svr1.postgresql.org (Postfix) with ESMTP id 72C9B8B9F04 for ; Fri, 11 Feb 2005 11:46:21 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-36.mail.demon.net with esmtp (Exim 4.42) id 1CzZFb-0006XL-Kc; Fri, 11 Feb 2005 11:46:19 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 6CF6617F76; Fri, 11 Feb 2005 11:46:14 +0000 (GMT) Message-ID: <420C9B07.7040809@archonet.com> Date: Fri, 11 Feb 2005 11:46:15 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: David Goodenough Cc: pgsql-general@postgresql.org Subject: Re: SQL query References: <200502111107.25004.david.goodenough@btconnect.com> In-Reply-To: <200502111107.25004.david.goodenough@btconnect.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.066 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200502/634 X-Sequence-Number: 73438 David Goodenough wrote: > I realise this is not strictly a Postgreslql question, but if the best way to > solve it involves using PG extensions, such as the PG procedural languages > I am only going to do this on PG and so I am happy to use them. > > 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. Not sure whether a schema change is possible for you, but you might want to have two tables - addresses (customer_id*, addr_id*, ...) addr_usage (customer_id*, addr_type*, addr_id) Add a custom trigger that ensures for every customer_id there is a valid row in addr_usage for each addr_type (sales, billing, shipping etc). That way you can have any mix of addresses you like, and it's explicit which address is for which purpose. -- Richard Huxton Archonet Ltd