I\'m using postgres 9.4, I have the following query:
SELECT pid, code, name FROM activity, ( SELECT code FROM project ) projects WHERE activity.pcode = project
select pid, json_agg(json_build_object('code',code,'name',name)) from ... group by pid
SELECT pid from activity a, ( select json_agg(project) from project where code = a.code ) as p
Something like that ...