I am not sure what you are asking...
SELECT CASE WHEN EXISTS (SELECT foo FROM bar WHERE baz = 'a') THEN 1
ELSE 0 END;
SELECT CASE WHEN 'a' = ANY (SELECT froo FROM bar) THEN 1 ELSE 0 END;
Both work, but that's pretty much what you had already - am I missing
what you are trying to achieve?
Though both are likely to be quite inefficient if you are looking up
Maybe something like:
SELECT f.a, CASE WHEN b.a IS NOT NULL THEN 1 ELSE 0 END
FROM foo f LEFT JOIN bar b USING (a)
Assuming "foo" has the values you want to look up, and "bar" is the
table you check for existence.
> -----Original Message-----
> From: pgsql-sql-owner(at)postgresql(dot)org
> [mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Lane Van Ingen
> Sent: Thursday, August 18, 2005 9:32 PM
> To: pgsql-sql(at)postgresql(dot)org
> Subject: [SQL] SQL CASE Statements
> In the following CASE statement, is it possible to put a
> SELECT ... WHERE EXISTS in the <condition> of a CASE
> statement, and have it work?
> The <condition> I want to do is to yield a result of '1' if
> the statement finds the value 'a' in a table (EXISTS
> evaluates true), and '0' if it evaluates false ('a' not found).
> SELECT a,
> CASE WHEN <CONDITION> THEN 1
> ELSE 0
> Has anybody done this? If so, can you send me a sample?
> ---------------------------(end of
> TIP 4: Have you searched our list archives?
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer
pgsql-sql by date
|Next:||From: Josh Berkus||Date: 2005-08-19 04:18:21|
|Subject: Re: dates and selection|
|Previous:||From: Lane Van Ingen||Date: 2005-08-19 01:31:34|
|Subject: SQL CASE Statements|