我有一个表,其中的列中包含以下值:
表格(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
该怎么做?
答案 0 :(得分:2)
使用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
答案 1 :(得分:1)
我会使用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;