简体   繁体   中英

Aggregate two columns and rows into one

I have the following table structure

start|end
09:00|11:00
13:00|14:00

I know

SELECT ARRAY_AGG(start), ARRAY_AGG(end)

Will result in

start|end
[09:00,13:00]|[11:00,14:00]

But how can i get the following result? result

[09:00,11:00,13:00,14:00]

BTW, I'm using Postgres

You could do array concatenation (if order is not important):

SELECT ARRAY_AGG(start) || ARRAY_AGG(end) FROM TABLE1

If order is important you could use Gordon's approach but :

  • add aggregate order array_agg(d order by d ASC)
  • use unnest instead of union all , because Gordon's solution ( union all ) performs two sequence scan. If table is big it could be better for performance to use:

     SELECT array_agg(d ORDER BY d ASC) FROM( SELECT unnest(ARRAY[start] || ARRAY[end]) as d from table1 ) sub 

which performs only one sequence scan on table (and will be faster).

One method is to unpivot them and then aggregate:

select array_agg(d)
from (select start as d from t
      union all
      select end as d from t
     ) t;

A similar method uses a cross join :

select array_agg(case when n.n = 1 then t.start else t.end end)
from t cross join
     (select 1 as n union all select 2) n;

I assume the start and end are character type

select ARRAY_AGG(col) 
from(select string_agg(strt::text||','||en::text,',') col 
     from b
)t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM