如何在postgres中找出数据类型为数组的ruby中元素的数量
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}
答案 0 :(得分:0)
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 |