Re: Split the result of a query in 2 rows

From: Frank Pinto <frank(at)ayalo(dot)co>
To: David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: PGSQL-Novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Split the result of a query in 2 rows
Date: 2015-03-04 19:06:41
Message-ID: CAATpuJrAAXz39vGFVA0CXLEe6Z6D=xZf8f8s3WmW_Fn2j6f9uQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Nice, thanks! You've inspired me to have a postgresql Vagrant setup on my
windows box to test quickly

Frank

On Wed, Mar 4, 2015 at 1:00 PM, David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com
> wrote:

> Frank Pinto wrote
> > So:
> > If the original result set returned 1 row with 2 columns the new solution
> > would return 2 rows with 1 column?
> > If the original result set returned 100 row with 3 columns the new
> > solution
> > would return 300 rows (1 row gets turned into 3 rows * 100 rows = 300)?
> >
> > I would use unnest
> > &lt;http://blog.lerner.co.il/turning-postgresql-arrays-rows-unnest/&gt;.
> > Something like this (untested):
> >
> > WITH temp_table AS (
> > SELECT ARRAY(SELECT field1, field2 FROM tbl_table ORDER BY field1) AS
> > prepared_fields;
> > )
> > SELECT UNNEST(prepared_fields) FROM temp_table;
> >
> > Note that's using one query using a CTE (
> > http://www.postgresql.org/docs/9.3/static/queries-with.html)
> >
> > Frank
>
> As noted, I'm pretty sure your query will not work as written but it did
> inspire the correct solution:
>
> SELECT unnest(ARRAY[f1, f2])
> FROM (VALUES (1,2), (3,4)) f (f1, f2)
>
> Note the difference between: ARRAY[val, val] and ARRAY(subquery)
>
> David J.
>
>
>
> --
> View this message in context:
> http://postgresql.nabble.com/Split-the-result-of-a-query-in-2-rows-tp5840497p5840509.html
> Sent from the PostgreSQL - novice mailing list archive at Nabble.com.
>
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice
>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Marcos Almeida Azevedo 2015-03-05 01:34:24 Re: Split the result of a query in 2 rows
Previous Message David G Johnston 2015-03-04 19:00:48 Re: Split the result of a query in 2 rows