Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 0F20E4759F2 for ; Wed, 28 Aug 2002 14:25:30 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 882C5475843 for ; Wed, 28 Aug 2002 14:25:29 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id D980AD610; Wed, 28 Aug 2002 11:25:28 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id CF33B5C02; Wed, 28 Aug 2002 11:25:28 -0700 (PDT) Date: Wed, 28 Aug 2002 11:25:28 -0700 (PDT) From: Stephan Szabo To: Tom Haddon Cc: Subject: Re: Case Statement In-Reply-To: Message-ID: <20020828112325.S87525-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/403 X-Sequence-Number: 9249 On Wed, 28 Aug 2002, Tom Haddon wrote: > Hi Folks, > > I am having troubles with a case statement in that I want to have the query > select only those records that match a particular case. Here's my query: > > SELECT > agency_contact_info.id,organization,department,city,state,description_of_ser > vices, CASE WHEN agency_contact_info.id > 0 THEN 0 ELSE 0 END + CASE WHEN > agency_contact_info.languages_other_text ~ 'Mien' THEN 1 ELSE 0 END AS > relevance > FROM agency_contact_info WHERE (agency_contact_info.guideregion=1 AND > list_online IS TRUE AND (agency_contact_info.id > 0 OR > agency_contact_info.languages_other_text ~ 'Mien' )) > ORDER BY relevance DESC, agency_contact_info.organization > > How do I add in the fact that I only want records where the CASE (as > relevance) > 0? I've tried using it in the WHERE statement adding a HAVING > statement and it doesn't like either. You will see a fair amount of I think you'll either need to duplicate the case statement or hide it in a subselect with the relevance check on the outer query (like select * from (select ... ) as a where relevance>0.