pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dmitri Bichko <dbichko@aveopharma.com>
To: Lane Van Ingen <lvaningen@esncc.com>
To: pgsql-sql@postgresql.org
Subject: Re: SQL CASE Statements
Date: Thu, 18 Aug 2005 21:56:17 -0400
Message-ID: <F18A6F7CF1661F46920F2CF713122FED46CC6E@mail.aveo.aveopharma.com> (raw)

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



view thread (5+ messages)  latest in thread

Message-ID: <F18A6F7CF1661F46920F2CF713122FED46CC6E@mail.aveo.aveopharma.com>
Permalink:  ../F18A6F7CF1661F46920F2CF713122FED46CC6E@mail.aveo.aveopharma.com/
Also on:    postgresql.org/message-id/F18A6F7CF1661F46920F2CF713122FED46CC6E@mail.aveo.aveopharma.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dbichko@aveopharma.com, lvaningen@esncc.com
  Subject: Re: SQL CASE Statements
  In-Reply-To: <F18A6F7CF1661F46920F2CF713122FED46CC6E@mail.aveo.aveopharma.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox