2017-02-19 24 views
0

以下のように配列の行を取得する再帰的なクエリがあります。どのようにして、すべての行を1つの行の1つの配列にマージし、重複を削除することができますか?注文は重要ではありません。Postgres配列集計行

--my_column-- 
"{431}" 
"{431,33}" 
"{431,60}" 
"{431,28}" 
"{431,1}" 
"{431,226}" 
"{431,38}" 
"{431,226,229}" 
"{431,226,227}" 
"{431,226,235}" 
"{431,226,239}" 
"{431,226,241}" 

私は以下のクエリを試してみましたが、私はunnest()から1つの空の整数[]列

select array(select unnest(my_column) from my_table 

おかげ

+0

あなたのリストに重複はありません。あなたはどんな結果を望んでいますか? –

答えて

2

distinctで使用array_agg()と(必要ない)order byを取得しています:

with my_table(my_column) as (
values 
    ('{431}'::int[]), 
    ('{431,33}'), 
    ('{431,60}'), 
    ('{431,28}'), 
    ('{431,1}'), 
    ('{431,226}'), 
    ('{431,38}'), 
    ('{431,226,229}'), 
    ('{431,226,227}'), 
    ('{431,226,235}'), 
    ('{431,226,239}'), 
    ('{431,226,241}') 
) 

select array_agg(distinct elem order by elem) 
from my_table, 
lateral unnest(my_column) elem; 

        array_agg     
--------------------------------------------- 
{1,28,33,38,60,226,227,229,235,239,241,431} 
(1 row) 
+0

ありがとうございました! – jimny

2

A nother solution without lateral subquery

select array_agg(distinct val) from 
    (select unnest(my_column) as val from my_table) x; 
+0

うまく働いた – jimny