Re: Split the result of a query in 2 rows

Поиск
Список
Период
Сортировка
От Frank Pinto
Тема Re: Split the result of a query in 2 rows
Дата
Msg-id CAATpuJqEgOET3CrrpJ7iRzMphoh-WK3N80jPaoF=96d7AMSb7Q@mail.gmail.com
обсуждение исходный текст
Ответ на Split the result of a query in 2 rows  (JORGE MALDONADO <jorgemal1960@gmail.com>)
Ответы Re: Split the result of a query in 2 rows  (David G Johnston <david.g.johnston@gmail.com>)
Re: Split the result of a query in 2 rows  (David G Johnston <david.g.johnston@gmail.com>)
Список pgsql-novice
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. 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

On Wed, Mar 4, 2015 at 12:28 PM, JORGE MALDONADO <jorgemal1960@gmail.com> wrote:
I have a very simple query to a single table as follows:

SELECT field1, field2 FROM tbl_table ORDER BY field1

Is it possible to "split" the results so field1 is displayed in one row and field2 in another row?

Best regards,
Jorge Maldonado

В списке pgsql-novice по дате отправления:

Предыдущее
От: Yaser Raja
Дата:
Сообщение: Re: Split the result of a query in 2 rows
Следующее
От: David G Johnston
Дата:
Сообщение: Re: Split the result of a query in 2 rows