Re: Select a column based on a value.

From: "Mark Wilson" <mark(at)mediasculpt(dot)com>
To: <pgsql-novice(at)postgresql(dot)org>, "eric soroos" <eric-psql(at)soroos(dot)net>
Subject: Re: Select a column based on a value.
Date: 2002-10-14 22:05:39
Message-ID: 002701c273cd$d8161150$3301a8c0@merl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi eric,

Is this what you are wanting?

select coalesce(form.titleField, form.title) as Value
from <stuff>

This will give you form.titleField, but if it is null, return form.title
instead.

----- Original Message -----
From: "eric soroos" <eric-psql(at)soroos(dot)net>
To: <pgsql-novice(at)postgresql(dot)org>
Sent: Tuesday, October 15, 2002 10:50 AM
Subject: [NOVICE] Select a column based on a value.

> I've got something like the following where each entry has an associated
form. (a join on entries.formNum=forms.objectNum)
>
> create table forms (objectNum int, title text, titleField text);
> create table entries (id serial, formNum int, col1 text, col2 text, ...);
>
> Forms have two types of names, either a static name in the title column or
a reference to a column name in the titleField column (i.e. col1, col2).
I'm looking for a way to have an embedded function pull out the title of the
entry.
>
> In psuedo code:
>
> entryTitle= if form.titleField is not null then entries.[form.titleField]
> else form.title
>
> I currently just grab all the possible columns and do the work in the
client, but I'd really like to be able to group by, sort and otherwise
manipulate this in the backend.
>
> Is there a way to do this? Or am I goign to run up against a hard wall
between code and data?
>
> eric
>
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-10-15 04:58:16 Re: test geometry ... FAILED
Previous Message eric soroos 2002-10-14 21:50:27 Select a column based on a value.