sql - How to get an array in postgres where the array size is greater than 1 -


i have table looks this:

val | fkey | num ------------------ 1   |  1   | 1 1   |  2   | 1   1   |  3   | 1   2   |  3   | 1  

what return set of rows in values grouped 'val', array of fkeys, array of fkeys greater 1. so, in above example, return like:

1 | [1,2,3] 

i have following query aggregates arrays:

select val, array_agg(fkey) mytable group val; 

but returns like:

1 | [1,2,3] 2 | [3] 

what best way of doing this? guess 1 possibility use existing query subquery, , sum / count on that, seems inefficient. feedback help!

use having clause filter groups having more fkey

select val, array_agg(fkey) mytable group val having count(fkey) > 1 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -