Suppose I have a table like this:
| id | linked_id | another_id |
|----|-----------|------------|
| 1 | 1 | 1 |
| 1 | 2 | null |
| 1 | 3 | 1 |
I would like to aggregate results by id - collect linked_ids and another_ids into a JSON objects. So in the end what I would like to get is this:
id: 1
linked_ids: [{ id: 1 }, { id: 2 }, { id: 3 }]
another_ids: [{ id: 1 }]
Notice how in the case of another_id only unique ids were aggregated and null got ignored. I'm able to achieve this with regular arrays by using:
array_agg(distinct another_id) filter (where another_id is not null).
However I need to get JSON array of objects in the end, not regular one. Any ideas?