Concatenate/merge array values during grouping/aggregation

前端 未结 2 1533
花落未央
花落未央 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:15

    The obvious solution would be the LATERAL join (which also suggested by @CraigRinger), but that is added to PostgreSQL in 9.3.

    In 9.1 you cannot avoid the sub-query, but you can simplify it:

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

    SQL Fiddle

提交回复
热议问题