When I have a column with separated values, I can use the unnest()
function:
myTable
id | elements
---+------------
1 |ab,cd,efg,hi
2 |jk,lm,n
unnest2()
as exerciseOlder versions before pg v8.4 need a user-defined unnest()
. We can adapt this old function to return elements with an index:
CREATE FUNCTION unnest2(anyarray)
RETURNS setof record AS
$BODY$
SELECT $1[i], i
FROM generate_series(array_lower($1,1),
array_upper($1,1)) i;
$BODY$ LANGUAGE sql IMMUTABLE;