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 BA4E88BA167 for ; Fri, 11 Feb 2005 16:45:39 +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 07445-08 for ; Fri, 11 Feb 2005 16:45:25 +0000 (GMT) Received: from smtp.uk.colt.net (smtp.uk.colt.net [195.110.64.125]) by svr1.postgresql.org (Postfix) with ESMTP id 196298B9E0D for ; Fri, 11 Feb 2005 16:45:24 +0000 (GMT) Received: from [10.0.1.4] (117-21-161-212.DSL.ONCOLT.COM [212.161.21.117]) by smtp.uk.colt.net (Postfix) with ESMTP id CA94CE2BD6 for ; Fri, 11 Feb 2005 16:38:03 +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:46:26 +0000 User-Agent: KMail/1.7.1 References: <200502111107.25004.david.goodenough@btconnect.com> <20050211133953.GB5110@wolff.to> In-Reply-To: <20050211133953.GB5110@wolff.to> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200502111646.26688.david.goodenough@btconnect.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.304 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE X-Spam-Level: X-Archive-Number: 200502/657 X-Sequence-Number: 73461 On Friday 11 February 2005 13:39, Bruno Wolff III wrote: > On Fri, Feb 11, 2005 at 11:07:24 +0000, > > David Goodenough wrote: > > 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. > > You can order by boolean expressions such as type = 'billing'. > You can use that with LIMIT or DISTINCT ON to get just the address you > want. Tried this, and got a rather un-intuative answer. If you have two relevant entries (one billing, the other default) and you:- order by type = 'billing' limit 1 you get the default one, if you:- order by type != 'billing' limit 1 you get the billing one. However:- order by type = 'billing' DESC limit 1 does get you the billing one. It makes sense in that false == 0 and true == 1 in many languages and 0 sorts before 1, but it still feels wrong. I had not realised I could use a comparison like this in order by. Thanks David