Stack Exchange
How to get counts in postgres column with datatype Array
How to find out count of elements in ruby having data type as array in postgres
Table in postgres
id | user_id | interests
---|---------|------------
1  | 1       | [1,2,3,4,5]
2  | 4       | [2,3]
3  | 2       | [2,5]


output: {1 => 1, 2 => 3, 3 => 2, 4 => 1, 5 => 2}


select val, count(*)
from (
        select unnest( z ) as val
        from (
            values
                    (1, 1, array[1,2,3,4,5]),
                    (2, 4, array[2,3]),
                    (3, 2, array[2,5])
            ) as x(x,y,z)
) x
group by val
order by 1

val |count |
----|------|
1   |1     |
2   |3     |
3   |2     |
4   |1     |
5   |2     |


Hey @krokodilko Thanks for your reply.. but I need values in hash using ruby code
