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 05C0F8B9E0D for ; Fri, 11 Feb 2005 16:40:35 +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 06884-09 for ; Fri, 11 Feb 2005 16:40:14 +0000 (GMT) Received: from smtp.uk.colt.net (smtp.uk.colt.net [195.110.64.125]) by svr1.postgresql.org (Postfix) with ESMTP id 1B07F8BA16C for ; Fri, 11 Feb 2005 16:39:41 +0000 (GMT) Received: from [10.0.1.4] (unknown [212.161.21.117]) by smtp.uk.colt.net (Postfix) with ESMTP id 4B41542886 for ; Fri, 11 Feb 2005 16:38:54 +0000 (GMT) From: David Goodenough Organization: D.G.A. ltd To: pgsql-general@postgresql.org Subject: Re: SQL query Date: Fri, 11 Feb 2005 16:39:05 +0000 User-Agent: KMail/1.7.1 References: <200502111107.25004.david.goodenough@btconnect.com> <420C979A.8070109@kynx.org> In-Reply-To: <420C979A.8070109@kynx.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200502111639.06071.david.goodenough@btconnect.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.262 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE X-Spam-Level: X-Archive-Number: 200502/654 X-Sequence-Number: 73458 On Friday 11 February 2005 11:31, Matt K wrote: > David Goodenough wrote: > >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. > > Use NULL to indicate that the customer type is default. Then you can > query with: > > select * from addresses where customer = ? > and coalesce(type, 'billing') = 'billing' > > If type is NULL, the comparison will be 'billing' = 'billing' - always > true. If there's a bunch of non-null type addresses, you'll get the > 'billing' one. > > http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html#A >EN12003 > > Matt > Well coalesce is not something I had come across, learn something every day. But I can not use this as the type (with the customer) are the primary key and therefore not null. I could do something like:- coalesce( nullif( 'default', type), 'billing') but I think that might be over egging it a bit. I will hope this one reserve and remember coalesce for the future. Thanks, David