Re: One column to multiple columns based on constraints? - Mailing list pgsql-general

From John R Pierce
Subject Re: One column to multiple columns based on constraints?
Date
Msg-id 4B72729D.7020302@hogranch.com
Whole thread Raw
In response to One column to multiple columns based on constraints?  ("Davor J." <DavorJ@live.com>)
List pgsql-general
Davor J. wrote:
> Let's say you have a table:
> CREATE TABLE t (
> time date,
> data integer
> )
>
> Suppose you want a new table that has columns similar to the following:
> "(x.time, x.data, y.time, y.data, z.time, z.data)" where x.time, y.time and
> z.time columns are constrained (for example x.time >2007  AND x.time <2008,
> y.time >2008 AND y.time < 2009, z.time > 2010)
>
> How would you do this. Note that you can not use JOIN as there is no
> relationship.
>
> Currently I came up with something like this:
>
> SELECT X.*, (SELECT Y.time, Y.data FROM t AS Y WHERE Y.time = X.time + 1),
> (SELECT Z.time .) FROM t AS X WHERE  X.time >2007  AND X.time <2008
>


Um, why can't you use a join?

SELECT X.*, Y.time, Y.data FROM t AS X JOIN t as Y ON (Y.time = X.time + '1 year'::INTERVAL)
    WHERE  X.time >= '2007-01-01'::DATE  AND X.time < '2008-01-01'::DATE;



I believe should be functionally equivalent to your nested select.   I'm
not real sure what you're trying to imply with your date > integer
comparisions, so I tried to be a little more rigorous there.



pgsql-general by date:

Previous
From: Carsten Kropf
Date:
Subject: Extending SQL in C using VARIABLE length type
Next
From: Allan Kamau
Date:
Subject: Re: xpath