2017-02-19 65 views
0

我有一個遞歸查詢,其中我得到如下所示的數組行。我怎麼可能將所有行合併到一行中的一個數組中並刪除重複項?訂購併不重要。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}" 

我想下面的查詢,但我得到一個空的整數[]列

select array(select unnest(my_column) from my_table 

感謝

+0

您的列表中沒有重複項。你想要什麼結果? –

回答

2

使用array_agg()distinct和(非必要)order byunnest()

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