postgresql - how to split a column that is created with array_agg -


i'm trying split arrayed column created using array_agg. following severely cut down, original query 210 lines

select   distinct on (visitor.id)  id,  array_agg(distinct item.code::text)   8xfullouter joins  exists( select distinct on(visitor.id) item.code::text 3 full outer join appear in first query  group visit.id,  item.code  order  visit.id ) 

i need break array_agg(distinct item.code::text) multiple columns. i've tried split_part(array_agg(distinct item.code::text), ',', 1) received following

> [err] error:  function split_part(character varying[], unknown, > integer) not exist line 159: split_part (array_agg(distinct > "public".procedure_group_cpt_... 

thanks!!!

use index of array, e.g.:

select (array_agg(code::text))[1]  (values ('code1'), ('code2')) codes(code)   array_agg  -----------  code1 (1 row) 

alternatively, can use string_agg() instead of array_agg(), e.g.:

select split_part(string_agg(code::text, ','), ',', 1) (values ('code1'), ('code2')) codes(code)   split_part  ------------  code1 (1 row)  

Comments