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:

    coalesce(nullif(array(Select x from unnest(array_agg( order by x where x is not null, '{}', '{}') as "contacts_agg"
    --, ^^ same logic as above 
from table as t 
group by 

How can I modify my query to group by state while aggregating all city and contact rows for each state?

  • 埋葬那份爱
    埋葬那份爱 回复


    select t.state, array_agg(distinct city) as city, array_agg(distinct contact) as contacts
    from t cross join
          regexp_split_to_table(contacts, ';') c(contact)
    group by t.state;

    Here is a db<>fiddle.