Skip site navigation (1) Skip section navigation (2)

Re: SELECT Question

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Alex <alex(at)meerkatsoft(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: SELECT Question
Date: 2003-08-31 18:21:20
Message-ID: 20030831111548.Y94333-100000@megazone.bigpanda.com (view raw or flat)
Thread:
Lists: pgsql-general
On Mon, 1 Sep 2003, Alex wrote:

> Hi,
>
> I need to form a query where i can add some columns based on the result.
>
>
> Table A
> ColA, ColB
> ----------
> 1      A
> 2      B
> 3      A
>
> Table B
> ColC
> ----
> A
>
> If A exists if would like the result back as
> 1  A   OK
> 2  B   NG
> 3  A   OK
>
> Is it possible to replace the value in the query ?


Maybe something like one of these:
 select cola, colb, case when not exists(select 1 from table_b where
  table_b.colc=table_a.colb) then 'NG' else 'OK' end
 from table_a;

 select cola, colb, case when colc is null then 'NG' else 'OK' end
 from table_a left outer join table_b on (table_a.colb=table_b.colc);

 select cola, colb, case when (select count(*) from table_b where
  table_b.colc=table_a.colb)=0 then 'NG' else 'OK' end
 from table_a;



In response to

pgsql-general by date

Next:From: Jeffrey MelloyDate: 2003-08-31 18:28:47
Subject: Re: SELECT Question
Previous:From: Stephan SzaboDate: 2003-08-31 18:15:19
Subject: Re: Quetions on Joins

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group