1

I have an SQL query that returns the data attached in the image. I would like to group the information and basically have just 2 rows (in this particular case). The row #1 would be: "evaluation_id": 1, "nombre": "Método: Jackson, Pollock & Ward" and "formulario" should be an array of json objects (in this case, records: 1,2,3 and 4). The row #2 would be: "evaluation_id": 2, "nombre": "Método: Medición Antropométrica Estándar" and "formulario" should be an array with only one json object (record #5).

evaluation_id nombre formulario
1 Jackson, Pollock & Ward [{json1},{json2},{json3},{json4}]
2 Medición Antropométrica Estándar [{json5}]

I have tried with functions like: array_to_json, json_object, json_aggr, json_array_elements but I couldn't make it work.

enter image description here

1 Answer 1

2

Please check the output of this query your expected

-- if use json type
select evaluation_id, nombre, json_agg(formulario)
from your_table
group by 1, 2

-- if use jsonb type
select evaluation_id, nombre, jsonb_agg(formulario)
from your_table
group by 1, 2
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @pooya, that works perfectly. What means the "group by 1, 2"?
Your welcome, group 1, 2 means group evaluation_id, nombre. You can use column number instead column name in group by
Oh, I didn't know that. Thanks for all your help @Pooya, I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.