X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7394856425 for ; Mon, 21 Mar 2005 14:29:37 +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 05056-10 for ; Mon, 21 Mar 2005 14:29:36 +0000 (GMT) Received: from sunsite.dcc.uchile.cl (sunsite.dcc.uchile.cl [192.80.24.2]) by svr1.postgresql.org (Postfix) with ESMTP id 34DCB56477 for ; Mon, 21 Mar 2005 14:29:30 +0000 (GMT) Received: from anakena.dcc.uchile.cl (anakena [192.80.24.6]) by sunsite.dcc.uchile.cl (8.12.11/8.12.11) with ESMTP id j2LETWg5021035; Mon, 21 Mar 2005 10:29:32 -0400 (CLT) Received: by anakena.dcc.uchile.cl (Postfix, from userid 4151) id E9C0A5236E; Mon, 21 Mar 2005 10:29:31 -0400 (CLT) Date: Mon, 21 Mar 2005 10:29:31 -0400 From: Alvaro Herrera To: =?iso-8859-1?Q?Mart=EDn_Marqu=E9s?= Cc: pgsql-sql@postgresql.org Subject: Re: CASE not working Message-ID: <20050321142931.GA11919@dcc.uchile.cl> References: <200503211118.39223.martin@bugs.unl.edu.ar> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <200503211118.39223.martin@bugs.unl.edu.ar> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.351 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE X-Spam-Level: X-Archive-Number: 200503/284 X-Sequence-Number: 20763 On Mon, Mar 21, 2005 at 11:18:38AM -0300, Martín Marqués wrote: Hey Martin, > I have this query which has a CASE in the middle to give me special results. > The problem is that it doesn't interpret my columns as it should. > > Here is the porblem: > > siprebi=> SELECT getvencimientosancion(190) AS vence, (SELECT codigo FROM > sanciones WHERE persona = (SELECT persona FROM usuarios WHERE codigo = 190) > ORDER BY femodif DESC LIMIT 1) AS sancion_original, CASE WHEN vence>=now() > THEN 1 ELSE 0 END AS sancionado; > ERROR: no existe la columna "vence" The problem is that the "vence" alias is not available at the time the CASE is evaluated. You need to use the getvencimientosancion() function, or put it in a subselect in case it's expensive to compute (or has side effects). -- Alvaro Herrera () "Java is clearly an example of a money oriented programming" (A. Stepanov)