This is jsonb field data:
[
{'name': 'pratha', 'email': '[email protected]', 'sub': { 'id': 1 } },
{'name': 'pratha', 'email': '[email protected]', 'sub': { 'id': 2 } }
]
When I want to get name and email fields, I get duplicate fields.
This is query:
SELECT jae.e->>'name', jae.e->>'email'
FROM survey_results sr
CROSS JOIN LATERAL jsonb_array_elements(sr.data_field) jae (e)
GROUP BY jae.e->>'name', jae.e->>'email'
In this data, both objects' name is pratha but email is different. What I want to is grab one pratha and latest email in array.
Is it possible?
Actual Result:
pratha [email protected]
pratha [email protected]
Expected Result:
pratha [email protected]
I only need name and email fields therefore, I want to group them by name.
See here: http://sqlfiddle.com/#!17/9b55f/2