Nurzhan Nogerbek
Nurzhan Nogerbek

Reputation: 5236

Select entries by list of ids from subquery?

In PostgreSQL database I make such SQL request:

SQL:

SELECT 
    ARRAY_AGG (QUESTION_ID) AS QUESTIONS 
FROM 
    factors_questions_relationship 
WHERE 
    FACTOR_ID IN (SELECT ARRAY_AGG (FACTOR_ID) AS FACTORS 
                  FROM surveys_factors_relationship 
                  WHERE SURVEY_ID = '9bef1274-f1ee-4879-a60e-16e94e88df38');

ERROR:

This SQL request raise error:
SQL Error [42883]: ERROR: operator does not exist: integer = integer[]
No operator matches the given name and argument types. You might need to add explicit type casts.

Second subquery return list of ids: {2,10,12,44,52}. I want to use that list of ids in main query. How to make it correctly?

Upvotes: 1

Views: 1957

Answers (1)

Fahmi
Fahmi

Reputation: 37473

You can try below -

SELECT ARRAY_AGG (QUESTION_ID) AS QUESTIONS FROM factors_questions_relationship 
where FACTOR_ID IN 
(
   SELECT FACTOR_ID AS FACTORS FROM surveys_factors_relationship 
   WHERE SURVEY_ID = '9bef1274-f1ee-4879-a60e-16e94e88df38'
)

Upvotes: 1

Related Questions