Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql - Opposite of string_agg

I'm looking for a postgresql function that will do the opposite of string_agg.

I have a movies table where the tags column contains values such as

Action|Adventure|Drama|Horror|Sci-Fi
Action|Horror|Sci-Fi

I would like to get a distinct list of tags from this column, for example

Action
Adventure
Drama
Horror
Sci-Fi
like image 960
Superdooperhero Avatar asked Mar 11 '17 14:03

Superdooperhero


2 Answers

You can use unnest() and string_to_array():

 select unnest(string_to_array(t.col, ','))
 from t
like image 143
Gordon Linoff Avatar answered Sep 29 '22 11:09

Gordon Linoff


You need string_to_array() combined with unnest()

select t.tag
from movies, unnest(string_to_array(tags,'|')) as t(tag)
like image 30
a_horse_with_no_name Avatar answered Sep 29 '22 10:09

a_horse_with_no_name