Re: Need help with embedded CASEs

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Denis Bucher <dbucher(at)niftycom(dot)com>
Cc: PGSQL-SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Need help with embedded CASEs
Date: 2001-11-07 18:06:36
Message-ID: 5450.1005156396@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Denis Bucher <dbucher(at)niftycom(dot)com> writes:
> SELECT CASE WHEN '2001-11-07' = current_date THEN 't' ELSE 'f' END AS
> flag_today, CASE WHEN flag_today THEN current_time ELSE '00:00' END AS
> time_iftoday;

> Why doesn't it work ?

flag_today is an output variable of this SELECT, not an input.

> Or how could I use the result of the CASE in another ?

AFAIK the only way to avoid writing the expression twice is to use
a sub-select:

SELECT ss.flag_today,
CASE WHEN ss.flag_today THEN current_time ELSE '00:00' END AS time_iftoday
FROM
(SELECT CASE WHEN '2001-11-07' = current_date THEN true ELSE false END AS
flag_today) AS ss;

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Nick Fankhauser 2001-11-07 18:29:44 Re: RIGHT JOIN is only supported with mergejoinable join conditions
Previous Message Tom Lane 2001-11-07 17:38:43 Re: RIGHT JOIN is only supported with mergejoinable join conditions