DISTINCT with two array_agg (or one array_agg with tuple inside)?
问题 I've got the following query: SELECT DISTINCT ON (ps.p) m.groundtruth, ps.p, ARRAY_AGG(m.anchor_id), ARRAY_AGG(m.id) FROM measurement m JOIN (SELECT unnest(point_array) AS p) AS ps ON ST_DWithin(ps.p, m.groundtruth, distance) GROUP BY ps.p, m.groundtruth ORDER BY ps.p, RANDOM() The output looks like that: groundtruth | p | anchor_array | id_array ------------------------------------------------------ G1 | P1 | {1,3,3,3,4} | {1,2,3,4,5} G2 | P1 | {1,5,7} | {6,7,8} G1 | P2 | {1,3,3,3,4} | {1,2