3

i am converting multiple rows in to a array using array_agg() function, and i need to give that array to a select statements where condition.

My query is,

SELECT * FROM table WHERE id = 
  ALL(SELECT array_agg(id) FROM table WHERE some_condition)

but it gives error, how can i over come it..

2
  • what is the error you are getting?? Commented Apr 5, 2013 at 6:23
  • ERROR:operator does not exist: bigint = bigint[] HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. Commented Apr 5, 2013 at 6:26

2 Answers 2

3

the error has been cleared by type casting the array, using my query like this

 SELECT * FROM table WHERE id = 
    ALL((SELECT array_agg(id) FROM table WHERE some_condition)::bigint[])

reference link

Sign up to request clarification or add additional context in comments.

Comments

1

It seems like you are over-complicating things. As far as I can tell, your query should be equivalent to simple:

SELECT * FROM table WHERE some_condition

Or, if you are selecting from 2 different tables, use join:

SELECT table1.*
FROM table1
JOIN table2 ON table1.id = table2.id
WHERE some_condition

Not only this is simpler, it is also faster than fiddling with arrays.

2 Comments

i need to take the count of the records,
this was not mentioned in your question. what's wrong with SELECT count(*) ...?

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.