I have a problem with my SQL request, it looks like this
SELECT
json_build_object('id', u.id,
'personnes_morale', json_build_object('denomination', pm.denomination),
'personne_physique', json_build_object('nom_patronyme', pp.nom_patronyme) ,
'representants', json_agg(json_build_object('name', r.name)),
'observations', json_agg(json_build_object('id', o.id))
)
FROM
dossiers_entreprises u
LEFT JOIN personnes_morales pm ON pm.numero_gestion = u.numero_gestion
LEFT JOIN personnes_physiques pp ON pp.numero_gestion = u.numero_gestion
LEFT JOIN representants r ON r.numero_gestion = u.numero_gestion
LEFT JOIN observations o ON o.numero_gestion = u.numero_gestion
WHERE
u."numero_gestion" = '1955B08131'
GROUP BY
u.id,pp.id,pm.id
LIMIT 1;
and it should give me something like this :
{
id:'123',
personne_morale:{denomination: 'test'},
personne_physique:{nom_patronyme: 'Smith'},
representants:[{name:'John'},{name:'Smith'}],
observations:[{id:'1'},{id:'2'}]
}
but because of i have two json_agg (i've test with just one fro representant and it works good) it return me something like :
{
id:'123',
... <- *personne_morale*
... <- *personne_physique*
representants:[{name:'John'},{name:'John'},{name:'Smith'},{name:'Smith'}]
observations:[{id:'1'},{id:'1'}{id:'2'},{id:'2'}]
}
it duplicate my 'representants' and 'observations' results and i don't understand why
Do you have any solutions
Thanks in advance