Слияние строк в postgresql - PullRequest
       17

Слияние строк в postgresql

0 голосов
/ 11 октября 2018

У меня есть таблица со следующими значениями в столбцах:

Таблица (col1, col2, col3, col4, col5, col6):

    a b c d e f
    a b c g h i
    a b c k l m
    a b c n o p

В результате я хочу иметь одну строку:

a b c d e f g h i k l m n o p

Как это сделать?

Ответы [ 2 ]

0 голосов
/ 11 октября 2018

Я бы использовал string_agg():

select string_agg(t.col, ' ') 
from (select col1 as col 
      from t
      union
      select col2 
      from t
      union
      . . .
      select col6 
      from t
     ) t;
0 голосов
/ 11 октября 2018

использовать union и string_agg

select string_agg(distinct c ,' ') from
   (
    select col1 as c from t
    union
    select col2 from t
    union
    select col3 from t
    union
    select col4 from t
    union
    select col5 from t
    union
    select col6  from t
  ) as t1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...