Alberto Blanco Cala
Alberto Blanco Cala

Reputation: 88

How to convert results of a group by into a json using POSTGRESQL?

I have the following question: If I have this data in a table:

enter image description here

How could you get a result like this:

enter image description here

It would be the same for me if the result came in a json like this:

enter image description here

Thank you!

Upvotes: 0

Views: 60

Answers (1)

user330315
user330315

Reputation:

To get the aggregated JSON you can use jsonb_object_agg()

select users, jsonb_object_agg(day, polls)
from data
group by users;

Online example

Upvotes: 1

Related Questions