I have 2 columns with a list of towns and I want to split the towns into rows
user 1 | [town1,town2,town3]
I want to split this into rows:
user 1 | town 1
user 1 | town 2
user 1 | town 3
Building on a_horse_with_no_name's answer, why not have something simpler like:
--Get some sample data
WITH test(username, town) as
(
select 'user 1', array['town1', 'town2', 'town3']
UNION
select 'user 2', array['town3', 'town2', 'town5']
)
--The following gives the results you are after.
SELECT username, unnest(town)
FROM test
I completely agree with using the unnest function, but I don't understand the reason for the cross join.
OP, it is very important what data type your "list" is stored as. Here I am assuming it is stored as an ARRAY. If it's stored as a string, then Rahul's answer will do the trick.