I have data in a table. I want to split the array data into separated rows.
create table test1 (
_id serial,
questionId character varying (50),
answer character varying (50),
subquestionId character varying (50),
subquestionAnswer character varying (50),
isActive character varying (1)
);
INSERT INTO test1 (questionid,answer,subquestionid,subquestionanswer,isactive)
values
('question 1','true','[100,101,102]','[[true],[false],[true]]','1'),('question 2','false','[101,106]','[[false],[true]]','1');
_id | questionid | answer | subquestionid | subquestionanswer | isactive |
---|---|---|---|---|---|
1 | question 1 | true | [100,101,102] | [[true],[false],[true]] | 1 |
2 | question 2 | false | [101,106] | [[false],[true]] | 1 |
3
Answers
Thank you for the reply!
Below table data needs to convert into screenshot format. Earlier there was one bracket one value, now there are one bracket two values. (subquestionid-153). I have extract this data from Talend.
As explained in this answer you may convert the string representation of arrays to real arrays with translate and cast. (As commented you should not store the arrays as strings)
result
To split the arrays use
unnest
and keep the order usingWITH ORDINALITY AS
Finaly limit the result to rows with the identical
ordinality
, which I assume is the expected result, though differing from your outputYou can do it using
string_to_array
to convert string to array, and generate multiple rows from single one as follows :this how it should be :
Demo here : https://dbfiddle.uk/b1w3RyCJ