开发者

Group by an array's element in PostgreSQL

In Postgres 8.3, I need to group by certain elements with开发者_开发知识库in 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".


Something like

GROUP BY bar[4]


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜