0

I am tring to insert data to PostgreSQL table from array. My next sql query raise error. Why I can't use ARRAY_AGG in UNNEST?

INSERT INTO surveys_questions_relationship(survey_id, question_id)
SELECT '9bef1274-f1ee-4879-a60e-16e94e88df38' ID, x
FROM UNNEST(
    SELECT ARRAY_AGG (QUESTION_ID)
    FROM factors_questions_relationship 
    WHERE FACTOR_ID = 10
) x

Subquery inside UNNEST return list of ids {1,2,3,4,5,6}. How correctly create array to UNNEST?

1
  • 2
    Why would you aggregate the values just to unnest them immediately after that? Commented Mar 27, 2019 at 7:31

1 Answer 1

2

I don't see the reason for aggregation or unnesting in your statement.

You can simply write:

INSERT INTO surveys_questions_relationship(survey_id, question_id)
SELECT '9bef1274-f1ee-4879-a60e-16e94e88df38', QUESTION_ID
FROM factors_questions_relationship 
WHERE FACTOR_ID = 10;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.