Concatenate/merge array values during grouping/aggregation

前端 未结 2 1531
花落未央
花落未央 2020-12-13 19:49

I have a table with the an array column type:

 title       tags
\"ridealong\";\"{comedy,other}\"
\"ridealong\";\"{comedy,tragedy}\"
\"freddyjason\";\"{horror         


        
2条回答
  •  一整个雨季
    2020-12-13 20:12

    Custom aggregate

    Approach 1: define a custom aggregate. Here's one I wrote earlier.

    CREATE TABLE my_test(title text, tags text[]);
    
    INSERT INTO my_test(title, tags) VALUES
    ('ridealong', '{comedy,other}'),
    ('ridealong', '{comedy,tragedy}'),
    ('freddyjason', '{horror,silliness}');
    
    CREATE AGGREGATE array_cat_agg(anyarray) (
      SFUNC=array_cat,
      STYPE=anyarray
    );
    
    select title, array_cat_agg(tags) from my_test group by title;
    

    LATERAL query

    ... or since you don't want to preserve order and want to deduplicate, you could use a LATERAL query like:

    SELECT title, array_agg(DISTINCT tag ORDER BY tag) 
    FROM my_test, unnest(tags) tag 
    GROUP BY title;
    

    in which case you don't need the custom aggregate. This one is probably a fair bit slower for big data sets due to the deduplication. Removing the ORDER BY if not required may help, though.

提交回复
热议问题