Using a query along with the functions available in postgres such as string_to_array
and string_agg
, data from raw tables are converted to following results set.
id, text
001, {foo,boo,foo}
002, {"",for,test,friday}
003, {"","",test,friday,tuesday,foo,boo}
Here id is id of a person and text is actually type of array. Now what I am trying to do is generate following structure.
id, text, text_count
001, foo, 2
001, boo, 1
002, test, 1
002, friday, 1
This is the query I used to get my existing format that I have mentioned, but how can I enhance this query to get the id, text, text_count results.
select id, string_to_array(string_agg(b.text,' '), ' ') as words
from tableA a,tableB b group by id
I would also like to get rid of data with "", I believe they are empty strings in postgres but not really sure.