Re: Hairy question - transpose columns

From: eric soroos <eric-psql(at)soroos(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Hairy question - transpose columns
Date: 2002-10-23 18:18:58
Message-ID: 47562919.1176750558@[4.42.179.151]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> I'm sure there's a better way, but I think a series of union alls would
> do it but be rather computationally expensive.
>
> select cod_var, Year, Month, 1 as Day, RainDay1 as Rain
> where Ten=1
> union all
> select cod_var, Year, Month, 2 as Day, RainDay2 as Rain
> where Ten=1

You could do the following:

select cod_var, Year, Month, 1+((ten-1)*10) as Day, RainDay1 as Rain
where RainDay1 is not null
union all
select cod_var, Year, Month, 2+((ten-1)*10) as Day, RainDay2 as Rain
where RainDay2 is not null
..


I'm sure that there is a function that could do this too, but I'd tend to just convert the data and be done with it.

eric

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2002-10-23 18:21:00 Re: plpgsql cursors : dynamic or static?
Previous Message Stephan Szabo 2002-10-23 18:06:29 Re: Hairy question - transpose columns