4

In Postgres 8.3, I need to group by certain elements within an array field. When I group by the array itself, all possible combinations are shown. I only want to group by the element.

something like this works for finding the count of a single element:

SELECT count(*)
FROM table
WHERE foo=any(bar)

this will return the correct count for a single element in an array. How do I return multiple counts for all elements in an array? If I group by the array, it will use all array elements in the identical order they are stored (not what I need).

edit for clarity: bar is an array with values like {foo, some, thing} or {foo, thing} or {some, thing, else}

I want to know how many records have an element value of foo, some, thing, and else in the array "bar".

2 Answers 2

1

Something like

GROUP BY bar[4]
Sign up to request clarification or add additional context in comments.

1 Comment

is this the actual syntax? I am running 8.3, and not getting it to work with your syntax. I will update my question with a better example.
0

Hm.. you can't group by a value.. but of course the value is the column that contains it... So.. you might want this:

SELECT AVG(blah), foo
FROM whatever
WHERE foo=any(bar)
GROUP BY foo

should work..

1 Comment

that doesn't work for me. if bar = {f,o,o} your example is: SELECT AVG(), f FROM whatever WHERE f=any(bar) group by f

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.