I am new to PostgreSQl and I am not able to find a solution to get the output mentioned below.
I have created a table with a column of data type text[], which is an array of text strings (single element).
My table looks like this:
col1 col2
ID1 {"P25963(MI:0326), Homo sapiens);O14920(MI:0326), Homo sapiens)"}
ID2 {"Q8NFZ0(MI:0326), Homo sapiens);P12931(MI:0326), Homo sapiens)"}
ID3 {"P26368(MI:0326), Homo sapiens);Q15637(MI:0326), Homo sapiens); Q15638(MI:0326), Homo sapiens)"}
When I try to access the first and second element of col2, I get below output.
SELECT col2[1] FROM table;
P25963(MI:0326), Homo sapiens);O14920(MI:0326), Homo sapiens
Q8NFZ0(MI:0326), Homo sapiens);P12931(MI:0326), Homo sapiens
P26368(MI:0326), Homo sapiens);Q15637(MI:0326), Homo sapiens); Q15638(MI:0326), Homo sapiens
SELECT col2[2] FROM table;
NULL
NULL
NULL
I would like to split this array element into multiple elements (presence of ;), unnest them and later perform some regex on the unnested rows.
The desired output after splitting is:
col1 col2
ID1 {P25963(MI:0326), Homo sapiens},{O14920(MI:0326), Homo sapiens}
ID2 {Q8NFZ0(MI:0326), Homo sapiens},{P12931(MI:0326), Homo sapiens}
ID3 {P26368(MI:0326), Homo sapiens},{Q15637(MI:0326), Homo sapiens}, {Q15638(MI:0326), Homo sapiens}
The desired output after unnesting is:
col1 col3 col4
ID1 P25963(MI:0326), Homo sapiens O14920(MI:0326), Homo sapiens
ID2 Q8NFZ0(MI:0326), Homo sapiens P12931(MI:0326), Homo sapiens
ID3 P26368(MI:0326), Homo sapiens Q15637(MI:0326), Homo sapiens
ID3 P26368(MI:0326), Homo sapiens Q15638(MI:0326), Homo sapiens
Any suggestion would be really helpful.
Thanks