select name, (regexp_split_to_array(name, '')) from table
The result is {a,b,c,d,e}
. Is it possible to split that up to individual rows and group by name so that it looks like:
a 1
b 2
c 3
d 4
e 5
select name, (regexp_split_to_array(name, '')) from table
The result is {a,b,c,d,e}
. Is it possible to split that up to individual rows and group by name so that it looks like:
a 1
b 2
c 3
d 4
e 5
Use unnest()
with row_number()
window function:
WITH test_table(name) AS ( VALUES
('abcde')
)
SELECT *,row_number() OVER () AS row FROM (
SELECT unnest(regexp_split_to_array(name,'')) AS name FROM test_table
) t;
Result:
name | row
------+-----
a | 1
b | 2
c | 3
d | 4
e | 5
(5 rows)
There is also great answer at SO:
If you are on 9.4 or later you can use with ordinality
:
SELECT name, u.*
FROM the_table
cross join lateral unnest(regexp_split_to_array(name,'')) with ordinality as u(ch, nr)