postgresql: Aggregating arrays of text -
i want array of words associated each id in example data:
id | words ---|------------------ 1 | {foo,bar,zap,bing} 2 | {foo} 1 | {bar,zap} 2 | {bing} 1 | {bing} 2 | {foo,bar} outputs:
id | allwords ---|-------------------------------- 1 | {foo,bar,zap,bing,bar,zap,bing} 2 | {foo,bing,foo,bar} i tried using array_agg(words) yields:
error: not find array type data type text[]
what's proper approach here? want words, duplicates.
array_agg collects results of column array; doesn't aggregate arrays.
array_cat functionality want, isn't aggregate function.
to define own aggregate based on it, use code:
create aggregate array_cat_aggregate (anyarray) ( sfunc = array_cat ,stype = anyarray ,initcond = '{}' ); which blatantly copied answer: https://stackoverflow.com/a/11763245/1394393. (i don't think question duplicate.)
pretending table called temp, can select using function group by:
select id, array_cat_aggregate(words) temp group id; here's sql fiddle you: http://sqlfiddle.com/#!12/7c828/1/0
Comments
Post a Comment