Re: CASE not working - Mailing list pgsql-sql

From Alvaro Herrera
Subject Re: CASE not working
Date
Msg-id 20050321142931.GA11919@dcc.uchile.cl
Whole thread Raw
In response to CASE not working  (Martín Marqués <martin@bugs.unl.edu.ar>)
Responses Re: CASE not working  (Martín Marqués <martin@bugs.unl.edu.ar>)
List pgsql-sql
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 (<alvherre[@]dcc.uchile.cl>)
"Java is clearly an example of a money oriented programming"  (A. Stepanov)


pgsql-sql by date:

Previous
From: Martín Marqués
Date:
Subject: CASE not working
Next
From: Thomas F.O'Connell
Date:
Subject: Re: query