I have such a table
id SERIAL,
user_id INT,
community_id INT[],
The table filled this way:
id | user_id | community_id
1 | 1 | {2, 4}
2 | 5 | {2, 5}
3 | 10 | {2, 4}
I'd like to get COUNT of users which each community has, community_id is array cuz user can be in several community at the same time.
The query should be simple as:
SELECT community_id, COUNT(user_id) FROM tbl GROUP BY community_id
The result should be like this:
community_id | user_count
2 | 3
4 | 2
5 | 1
I do not know how to GROUP BY array column. Can anybody help me ?