Thread: Looking for correct SQL

Looking for correct SQL

From
Alexander Pucher
Date:
Hi,

I'm struggling for some time with the following problem:

I have a table like this:

name    key            value
-------------------------
andi    age                23
andi    hometown    boston
tom    age                45
greg    hometown    miami


I would like to get the following through a SQL Select Statement:


name    age    hometown
-------------------------
andy    23        boston
tom      45         NULL
greg      NULL   miami

I thought about a subselect, but could'nt find the clue.

Thanks for any input,
best regards,
alex.

Re: Looking for correct SQL

From
Michael Fuhr
Date:
On Thu, Oct 14, 2004 at 09:46:33PM +0200, Alexander Pucher wrote:
>
> I have a table like this:
>
> name    key            value
> -------------------------
> andi    age                23
> andi    hometown    boston
> tom    age                45
> greg    hometown    miami
>
>
> I would like to get the following through a SQL Select Statement:
>
>
> name    age    hometown
> -------------------------
> andy    23        boston
> tom      45         NULL
> greg      NULL   miami

Take a look at the crosstab functions in contrib/tablefunc.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/