I am not sure what you are asking...
SELECT CASE WHEN EXISTS (SELECT foo FROM bar WHERE baz = 'a') THEN 1
ELSE 0 END;
Or
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
many values.
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.
Dmitri
> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org
> [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Lane Van Ingen
> Sent: Thursday, August 18, 2005 9:32 PM
> To: pgsql-sql@postgresql.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
> END
>
> Has anybody done this? If so, can you send me a sample?
>
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Have you searched our list archives?
> http://archives.postgresql.org
The information transmitted is intended only for the person or entity to which it is addressed and may contain
confidentialand/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any
actionin reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you
receivedthis in error, please contact the sender and delete the material from any computer