I got the following postgresql tables:
Table "public.dates"
Column | Type
----------------+-------------------------
id | integer
start | timestamp with time zone
end | timestamp with time zone
Table "public.date_participants"
Column | Type
---------+--------
date_id | integer
user_id | integer
I want to get a date with all its participants as an array. For example:
{ id: 252, start: xyz, end yzx, participants: [23, 51, 63, 67] }
So I created the following query:
SELECT
dates.id,
json_build_array(participants) as participants
FROM
dates,
(SELECT date_id, user_id FROM date_participants) as participants
WHERE dates.id=participants.date_id AND dates.ground_event_id = 252;
But this results in:
id | participants
----+--------------------------------
252 | [{"date_id":252,"user_id":2}]
252 | [{"date_id":252,"user_id":191}]
252 | [{"date_id":252,"user_id":581}]
252 | [{"date_id":252,"user_id":582}]
(4 rows)
How to combine these rows into one row with all its user_ids as participants?