DB table looks like this:
state | city | contact
---------------------------
NY | city1 | person1;person2;person3
NY | city2 | person4;person5;person6
NY | city3 | null
CA | city1 | person7;person8;person9
CA | city2 | person10;person11;person12
I want to group by state and turn city
into an array and split contact
on semicolon to turn into an array:
state | city. | contact
------------------------------------------------
NY | {city1, city2, city3} | {person1,person2,person3,person4,person5,person6,null}
CA | {city1, city2} | {person7,person8,person9,person10,person11,person12}
This aggregates contacts
for each state into 1 row and should handle null values, but it's not being split on semicolons:
select
t.state,
coalesce(nullif(array(Select x from unnest(array_agg(t.contact order by t.city)) x where x is not null, '{}', '{}') as "contacts_agg"
-- t.city, ^^ same logic as above
from table as t
group by
t.state
How can I modify my query to group by state
while aggregating all city
and contact
rows for each state?