Re: SQL CASE Statements

From: "Dmitri Bichko" <dbichko(at)aveopharma(dot)com>
To: "Lane Van Ingen" <lvaningen(at)esncc(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: SQL CASE Statements
Date: 2005-08-19 01:56:17
Message-ID: F18A6F7CF1661F46920F2CF713122FED46CC6E@mail.aveo.aveopharma.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(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
> 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 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

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2005-08-19 04:18:21 Re: dates and selection
Previous Message Lane Van Ingen 2005-08-19 01:31:34 SQL CASE Statements