Re: CASE SELECT syntax

From: Frank Miles <fpm(at)u(dot)washington(dot)edu>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Marco Lazzeri <marcomail(at)noze(dot)it>, pgsql-general(at)postgresql(dot)org
Subject: Re: CASE SELECT syntax
Date: 2004-01-15 19:59:23
Message-ID: Pine.A41.4.58.0401151157280.57350@homer36.u.washington.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 15 Jan 2004, Bruno Wolff III wrote:

> On Thu, Jan 15, 2004 at 18:23:47 +0100,
> Marco Lazzeri <marcomail(at)noze(dot)it> wrote:
> > What about a SELECT syntax like that?
> >
> > SELECT
> > CASE WHEN bar.foo_id IS NULL THEN bar.*
> > ELSE foo.*
> > FROM foo, bar
> >
> > Anyway, I need a SELECT query that gets data from a FIRST TABLE if a
> > specific value in a SECOND TABLE is NULL or from SECOND TABLE itself if
> > the value IS NOT NULL.
> >
> > Any suggestion?
>
> You almost certainly want some join condition between foo and bar.
> You can't actually use *. You will need a CASE for each column.
> CASE statements need and END.

Alternatively (though it fails to use the 'CASE' syntax) would be to use
COALESCE. Something like:

SELECT COALESCE((SELECT foo_id FROM table2 WHERE ...),
(SELECT bar_id FROM table1 WHERE ...));

HTH
-frank

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ralph Spitzer 2004-01-15 20:39:54 Survey: Motivations to Contribute
Previous Message Richard Huxton 2004-01-15 19:23:09 Re: Bug and/or feature? Complex data types in tables...