I have a followup to this question in which I generated arrays of this type of table:
val | fkey | num
------------------
1 | 1 | 10
1 | 2 | 9
1 | 3 | 8
2 | 3 | 1
In which the resulting returned row would like something like this (fkeys were essentially aggregated to one list):
1 | [1,2,3]
What I would like to do is modify the query with respective to the value in the 'num' column. That is, I would like something like:
1 | [1,2] | [10, 9]
1 | [1,3] | [10, 8]
1 | [2,3] | [9, 8]
The ordering of the third column in the return query doesn't bother me. Right now I have something like this:
SELECT val, array_agg(fkey), array_agg(num)
FROM mytable
GROUP BY val
Having Count(fkey) > 1
But that returns something more like:
1 | [1,2,3] | [10, 9, 8]
Which would be ok, except I can't easily tell which number in the third array comes from what fkey (if that makese sense). Something like this would work to keep track of it:
1 | [1,2,3] | [10 - 1, 9 - 2, 8 - 3]
I'm not sure what the best way to go about doing this is, but I'm open to suggestions.
EDIT: I'm on Postgres 9.3.6. The table definition is:
awesome-db=# \d mytable
Table "public.mytable"
Column | Type | Modifiers
----------+---------+-----------
val | bytea | not null
fkey | uuid | not null
num | integer | not null
Indexes:
"comp_key" UNIQUE CONSTRAINT, btree (fkey, num, val)
"fingerprint_index" btree (val)