Nurzhan Nogerbek
Nurzhan Nogerbek

Reputation: 5236

Is it possible to use array_agg result in unnest? | PostgreSQL

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?

Upvotes: 0

Views: 497

Answers (1)

user330315
user330315

Reputation:

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;

Upvotes: 2

Related Questions