What I'm trying to achieve
First I need to query table tableY to get all userids that fulfill the inner WHERE condition. Then I aggregate it into an array of userids with array_agg(userid). Then in the outer query, I need to select users from the tableX table with userids that exist inside the array I created before from tableY.
Error
I get the following error:
ERROR: operator does not exist: integer = integer[]
LINE 2: WHERE 3 IN ((
HINT: No operator matches the given name and argument types. You might need to add explicit type cast.
My query:
SELECT * FROM mydb.tableX
WHERE 3 IN ((
SELECT array_agg(userid) userids FROM
(
SELECT
DISTINCT(uc.userid), eui.firstname
FROM mydb.tableY uc
JOIN mydb.tableX eui ON uc.userid = eui.auth_userid
WHERE uc.level = 4
AND uc.subjectid = 1
AND uc.lineid = 5
GROUP BY uc.userid, eui.firstname
ORDER BY eui.firstname
) AS userids
))
Btw, I only use the "3" as a hard coded example now to get the query running in the first place.
Question
Why do I get the error?
Thanks!
INcan also be used with arrays,IN (SELECT …)checks whether any of the results of the subquery match the operand. Just omit thearray_agg(…)call and it should work.